Questions on XML

Stefan Behnel stefan_ml at behnel.de
Sat Aug 22 06:32:52 EDT 2009


Kee Nethery wrote:
> As a newbie, the thing that caused me trouble was importing a string
> into the XML parser.

That would be

	root_element = et.fromstring(some_string)

The parse() function is meant to parse from a file.


> from xml.etree import ElementTree as et
> theXmlDataTree = et.parse(StringIO.StringIO(theXmlString))
> 
> from xml.etree import ElementTree as et
> theXmlDataTree = et.ElementTree(et.XML(theXmlString))

You can use both, but I suspect parsing from StringIO to be slower than
parsing from the string directly. That's the case for lxml, at least.

Note that fromstring() behaves the same as XML(), but it reads better when
parsing from a string variable. XML() reads better when parsing from a
literal string.

Stefan



More information about the Python-list mailing list