Element Tree Help

R

Robert Rawlins

Hello Guys,I have little to no experiance with element tree and I'm struggling to find a way to parse the details from the XML document (attached) into my application. Essentialy I'm looking to take the following document and turn it into a dict of tuples, each dict element defines a datasource with the key to the element being the 'name' which is defined in the XML and then the value of the pair is a tuple which contains the details of the datasource, like the host and port etc.I've attached a copy of the example XML to this email.Can anyone offer some advice on how to get started with this? I've spent a little time looking over the documentation of element tree and have struggled to break the ice and parse this document.Thanks guys,Robert
_________________________________________________________________
The next generation of Windows Live is here
http://www.windowslive.co.uk/get-live
 
S

Stefan Behnel

Robert said:
Hello Guys,I have little to no experiance with element tree and I'm
struggling to find a way to parse the details from the XML document
(attached) into my application. Essentialy I'm looking to take the
following document and turn it into a dict of tuples, each dict element
defines a datasource with the key to the element being the 'name' which is
defined in the XML and then the value of the pair is a tuple which contains
the details of the datasource, like the host and port etc.I've attached a
copy of the example XML to this email.Can anyone offer some advice on how
to get started with this? I've spent a little time looking over the
documentation of element tree and have struggled to break the ice and parse
this document.Thanks guys,Robert

Given this document:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE datasources SYSTEM "datasources.dtd">
<datasources>
<datasource>
<name>a name</name>
<host>localhost</host>
<port>3306</port>
<database>database1</database>
<username>someusername</username>
<password>somepassword</password>
</datasource>
<datasource>
<name>another name</name>
<host>localhost</host>
<port>3306</port>
<database>database2</database>
<username>itsusername</username>
<password>andthepassword</password>
</datasource>
</datasources>

I would do something like this:

d = {}
for event, element in ET.iterparse("thefile.xml"):
if element.tag == "datasource":
d[element.find("name")] = tuple([ el.text for el in element ])

Stefan
 
S

Stefan Behnel

Stefan said:
Robert said:
Hello Guys,I have little to no experiance with element tree and I'm
struggling to find a way to parse the details from the XML document
(attached) into my application. Essentialy I'm looking to take the
following document and turn it into a dict of tuples, each dict element
defines a datasource with the key to the element being the 'name' which is
defined in the XML and then the value of the pair is a tuple which contains
the details of the datasource, like the host and port etc.I've attached a
copy of the example XML to this email.Can anyone offer some advice on how
to get started with this? I've spent a little time looking over the
documentation of element tree and have struggled to break the ice and parse
this document.Thanks guys,Robert

Given this document:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE datasources SYSTEM "datasources.dtd">
<datasources>
<datasource>
<name>a name</name>
<host>localhost</host>
<port>3306</port>
<database>database1</database>
<username>someusername</username>
<password>somepassword</password>
</datasource>
<datasource>
<name>another name</name>
<host>localhost</host>
<port>3306</port>
<database>database2</database>
<username>itsusername</username>
<password>andthepassword</password>
</datasource>
</datasources>

I would do something like this:

d = {}
for event, element in ET.iterparse("thefile.xml"):
if element.tag == "datasource":
d[element.find("name")] = tuple([ el.text for el in element ])

Sorry, the last line should be

d[element.findtext("name")] = tuple([ el.text for el in element ])

but you'll likely have to modify that line anyway.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top