tags:

views:

36

answers:

4

Hi

I'm trying to find a xml-interpret function (like the simplexml_load_string) in Python, but with no success :/

Let's say I have xml in a string

my_xml_string = """
<root>
      <content>
        <one>A value</one>
        <two>Here goes for ...</two>
      </content>
</root>"""

To read an value in php I would normaly do something like this

// read into object
$xml = simplexml_load_string(my_xml_string);

// print some values
echo $xml->root->content->one
echo $xml->root->content->two

are there any equivalent object in python/django?

Thanks

+1  A: 

The nearest is probably ElementTree which is part of the python standard library (or an extended version lxml)

import  xml.etree

element =  xml.etree.ElementTree.XML(my_xml_string)

sets up element which is of class Element and this can be treated as lists of XML elements

e.g.

# for your example
print(element[0][0].tag)
print(element[0][0].text)
print(element[0][3].text)

You can also search by XPaths if you want to use names.

lxml also has an objectify model that allows access of elements as "if you were dealing with a normal Python object hierarchy." Which matches the php useage more exactly

Mark
Hmm what about xml.dom.minidom?
Trikks
You could use that - but youare then manipularing a DOM - lxml and Element Tree try to make the navifaction more 'pythonic' not DOM specific
Mark
A: 

The Python standard library includes several xml parsing modules. Probably the easiest is ElementTree.

from xml.etree import cElementTree as ET
xml = ET.fromstring(my_xml_string)

print xml.find('.//content/one').text
print xml.find('.//content/two').text
Daniel Roseman
A: 

ElementTree is quite common and is probably the best library included in Python (since version 2.5).

However, personally I prefer lxml for both power and flexibility. The "lxml.objectify" method is particularly useful for parsing large XML DOMs into pythonic objects.

Gabriel Hurley
I dont really like the ElementTree, maybe i'm a bit unused to the syntax. xml.dom.minidom seems more complete to me. I'll definitely look into lxml! Thanks!
Trikks
minidom is the simplest, but also the *least* pythonic and the least feature-complete. I've used it to get the job done before, but if you spend much time working with XML you'll abandon it pretty soon.
Gabriel Hurley
A: 

from xml.dom.minidom import *

my_xml_string = """
<root>
      <content>
        <one>A value</one>
        <two>Here goes for ...</two>
      </content>
</root>"""

xml = parseString(xml_string)
result = xml.getElementsByTagName('one')[0].firstChild.data

This did the trick, for now!

Trikks