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

Kee Nethery kee at kagi.com
Thu Jun 25 21:02:25 EDT 2009


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?

Details:
I have a CGI that receives XML via an HTTP POST as a POST variable  
named 'theXml'. The POST data is a string that the CGI receives, it is  
not a file on a hard disk.

The POSTed string looks like this when viewed in pretty format:

<xml>
	<purchase id="1" lang="en">
		<item id="1" productId="369369">
			<name>Autumn</name>
			<quantity>1</quantity>
			<price>8.46</price>
		</item>
		<javascript>YES</javascript>
	</purchase>
	<customer id="123456" time="1227449322">
		<shipping>
			<street>19 Any Street</street>
			<city>Berkeley</city>
			<state>California</state>
			<zip>12345</zip>
			<country>People's Republic of Berkeley</country>
			<name>Jon Roberts</name>
		</shipping>
		<email>jumbo at shrimp.edu</email>
	</customer>
</xml>


The pseudocode in Python 2.6.2 looks like:

import xml.etree.ElementTree as et

formPostData = cgi.FieldStorage()
theXmlData = formPostData['theXml'].value
theXmlDataTree = et.XML(theXmlData)

and when this runs, theXmlDataTree is set to:

theXmlDataTree	instance	<Element xml at 7167b0>
	attrib	dict	{}
	tag	str	xml
	tail	NoneType	None
	text	NoneType	None

I get the same result with fromstring:

formPostData = cgi.FieldStorage()
theXmlData = formPostData['theXml'].value
theXmlDataTree = et.fromstring(theXmlData)

I can put the xml in a file and reference the file by it's URL and use:

et.parse(urllib.urlopen(theUrl))

and that will set theXmlDataTree to:

theXmlDataTree	instance	<xml.etree.ElementTree.ElementTree instance at  
0x67cb48>

This result I can play with. It contains all the XML.

et.parse seems to pull in the entire XML document and give me  
something to play with whereas et.XML and et.fromstring do not.

Questions:
How do I get this to work?
Where in the docs did it give me an example of how to make this work  
(what did I miss from reading the docs)?

... and for bonus points ...

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))

Thanks in advance,
Kee Nethery



More information about the Python-list mailing list