473,626 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple XML-to-Python conversion

I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures. I came across gnosis
XML tools, but need a built-in option for doing something similar.

My knowledge of DOM and anything beyond simple XML structures is
rudimentary at best. Is there a simple way to use SAX handlers to pull
attributes and elements into Python lists/dictionaries?

Jul 18 '05 #1
19 3296
ga********@gmai l.com schrieb:
I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures.


Take a look at Amara (http://uche.ogbuji.net/tech/4Suite/amara/).

Lutz
--
pub 1024D/6EBDA359 1999-09-20 Lutz Horn <lu*******@gmx. de>
Key fingerprint = 438D 31FC 9300 CED0 1CDE A19D CD0F 9CA2 6EBD A359
http://purl.oclc.org/NET/lutz.horn
http://www.spreadfirefox.com/?q=user/register&r=200
Jul 18 '05 #2
Amara does indeed make it effortless to transform an XML document into
a Python structure. Unfortunately this suggestion requires the 3rd
party software, Amara, _and_ a 4Suite installation according to the
website.

The reason I can't expect users to have 3rd party tools is because this
tool will be used in a secure lab environment without Internet access.
Asking the admins to place these software packages on the network for
users to install is like asking GW to say something semi-intelligent.

Jul 18 '05 #3
ga********@gmai l.com wrote:
Amara does indeed make it effortless to transform an XML document into
a Python structure. Unfortunately this suggestion requires the 3rd
party software, Amara, _and_ a 4Suite installation according to the
website.

The reason I can't expect users to have 3rd party tools is because this
tool will be used in a secure lab environment without Internet access.
Asking the admins to place these software packages on the network for
users to install is like asking GW to say something semi-intelligent.


Why not use distutils to install any additional packages you need? That's
what it's there for. Presumably you're going to need to package this thing
for distribution anyway.

--
Website: www DOT jarmania FULLSTOP com
Jul 18 '05 #4
A good question... Here's a followup question:

Does third-party software compile as well as the built-in modules when
using distutils and the py2exe extension module?

Jul 18 '05 #5
Am Wed, 16 Mar 2005 14:19:49 -0800 schrieb ga********@gmai l.com:
I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures. I came across gnosis
XML tools, but need a built-in option for doing something similar.

My knowledge of DOM and anything beyond simple XML structures is
rudimentary at best. Is there a simple way to use SAX handlers to pull
attributes and elements into Python lists/dictionaries?


Hi,

Just write a script which uses xml.sax. It is not difficult.
You get an event for every start-tag and for every end-tag.
The class which handles the events can build the structure
if your XML file.

The online version of the python cookbook has some
python and SAX examples,

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #6
"ga********@gma il.com" wrote:
I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures. I came across gnosis
XML tools, but need a built-in option for doing something similar.

My knowledge of DOM and anything beyond simple XML structures is
rudimentary at best. Is there a simple way to use SAX handlers to pull
attributes and elements into Python lists/dictionaries?


tools like ElementTree and xmltramp are very easy to use. ElementTree builds
a tree of Element objects which behave like lists (of subelements) and contain
dictionaries (of attributes). xmltramp builds a tree where both subelements and
attributes are mapped to object attributes.

http://www.aaronsw.com/2002/xmltramp/
http://www.effbot.org/zone/element-index.htm

if you want to ship any of them with your application, you only need a single
module (xmltramp.py and ElementTree.py, respectively).

API summaries:

http://reagle.org/joseph/blog/techno...menttree-model
http://reagle.org/joseph/blog/techno...xmltramp-model

(xmltramp might be a bit easier to use for simple cases, elementtree is a bit more
flexible and a bit more efficient, especially if you're using the C implementation)

</F>

Jul 18 '05 #7
On Thu, 17 Mar 2005 12:39:42 +0100, Lutz Horn <lu*******@gmx. de> wrote:
ga********@gmai l.com schrieb:
I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures.


If using XML is not absolutely essential, you might want to take a
look at YAML[1, 2].

[1] http://yaml.org/
[2] http://python.yaml.org/dist/

--
Premshree Pillai
http://www.livejournal.com/users/premshree/
Jul 18 '05 #8
Lutz Horn wrote:
ga********@gmai l.com schrieb:
I've been searching high and low for a way to simply convert a small
XML configuration file to Python data structures.

Take a look at Amara (http://uche.ogbuji.net/tech/4Suite/amara/).

Lutz


Amara rules xml to python conversions. I've used it successfully for an
xml interface to legacy databases.

Huy
Jul 18 '05 #9
Since I've exhausted every option except for Amara, I've decided to
give it a try. However, this will only work if I can compile Amara and
4suite along with my application. I doubt 4suite will be able to be
compiled, but I'll try it anyway.

If I weren't set on using XML (I know, not every application requires
it and it's abused), I would probably go with a simpler format like an
INI file or YAML. The requirement that leads me to use XML for a
simple config file is that the rest of this project already utilizes
XML and another developer must integrate his NetBeans GUI with my app
and config file. Not to mention that this must be across Linux and
Windows platforms. To make this easier on everyone else, I'm using a
simple XML schema that I know everyone can write to. As far as I know,
YAML isn't easily written to from a Java class and an INI file isn't
very popular on the Linux side.

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2268
by: chris | last post by:
Hi, After going back through the XSL tutorials and the help here I have largely solved the problem of merging two XHTML files, but one small detail remains... The parser outputs <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">
1
1340
by: dSchwartz | last post by:
I'm creating xml files from a dataset: myDataSet.WriteXml(m_XmlFile, XmlWriteMode.IgnoreSchema); and saving them to disk. Then i want to use the Xml control to format this xml with xsl stylesheet. here is part of my xml file created by the line above: <?xml version="1.0" standalone="yes" ?> <NewDataSet> <articles>
2
4628
by: John Estess | last post by:
The following xml file is converted by the piece of xml below (oil.xsl). I have the right number of dots for the unordered list items, but it won't print out the "use's". What am I doing wrong? XML scrap: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="oil.xsl"?> <oils>
3
2307
by: lars | last post by:
I would expected that the stylesheet below would tag the contents of the element "Date" with <b> and </b>, but it doesn't, at least not with xsltproc. It just passes the content through unchanged. I've also tried diffent permutations of upper and lower case. What have I missed? -Lars
4
1145
by: Tony G | last post by:
Hi - My experience with XML is limitted although I understand the basics, and I just recently for the first time created an XML document from a database query and styled it successfully with CSS. I have the DTD and the XML included in one document, and the style sheet is linked to. (I would cut and paste the text here - but the damn server is down due to the general incompetence of our network team and I have not backed
0
1265
by: Randy | last post by:
Is there a dotnet class that formats XML simple types. I'm making a xmldocument which has a timestamp element (among others). The format is: yyyy-MM-ddThh:mm:ss (which looks like 2003-09-28T07:55:06). The schema looks like: <xsd:simpleType name="TimestampType"> <xsd:annotation> <xsd:documentation>Base typefor a date and time stamp</xsd:documentation> </xsd:annotation>
6
4240
by: Mark Jerde | last post by:
We have developed about 80 XML documents, each probably 150 lines long, that are all supposed to conform to a schema. My understanding is the way to show that an XML document conforms to a schema is to include a reference to the XSD in the XML document, and check that it is valid. Some other members of the project team don't want to include a reference to the XSD in the XML documents, citing possible differences in parsers. Is my...
4
1741
by: MSSQLServerDeveloper | last post by:
I have the following xml file - <?xml version="1.0" encoding="utf-8" ?> <Publisher1>Mary</Publisher1> <Distributor>Joe</Distributor> and snippet of vb.net code that is failing with the error- "The <Publisher1> tag from namespace is not expected" Dim XmlReader As New XmlTextReader("ReplConfig.xml") Return XmlReader.ReadElementString("Distributor")
1
2229
by: shailesh333 | last post by:
I have a simple xsd file with me. How do I create xml instance of this xsd file.
3
1881
by: whitep8 | last post by:
Hi all, im a little bit puzzled with this one. Im after creating a simplexml encoded file using php, which calls from a mysql database. i have the following, but i dont see what is wrong with the syntax. <?php
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.