ElementTree.XML(string XML) and ElementTree.fromstring(string XML) not working

Nobody nobody at nowhere.com
Fri Jun 26 04:38:14 CEST 2009


On Thu, 25 Jun 2009 18:02:25 -0700, Kee Nethery wrote:

> Summary: I have XML as string and I want to pull it into ElementTree  
> so that I can play with it but it is not working for me. XML and  
> fromstring when used with a string do not do the same thing as parse  
> does with a file. How do I get this to work?

Why do you need an ElementTree rather than an Element? XML(string) returns
the root element, as if you had used et.parse(f).getroot(). You can turn
this into an ElementTree with e.g. et.ElementTree(XML(string)).

> Why isn't et.parse the only way to do this? Why have XML or fromstring  
> at all? Why not enhance parse and deprecate XML and fromstring with  
> something like:
> 
> formPostData = cgi.FieldStorage()
> theXmlData = formPostData['theXml'].value
> theXmlDataTree = 
> et.parse(makeThisUnicodeStringLookLikeAFileSoParseWillDealWithIt(theXmlData))

If you want to treat a string as a file, use StringIO.




More information about the Python-list mailing list