On Jun 25, 9:02 pm, Kee Nethery <k...@kagi.com> 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?
>
> 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:
[...]
> 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)?
>
[skipping bonus points question]

I'm not sure what you're expecting.  It looks to me like things are
working okay:

My test script:

import xml.etree.ElementTree as ET

data="""<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>ju...@shrimp.edu</email>
        </customer>
</xml>"""

xml = ET.fromstring( data )

print xml
print "attrib   ", xml.attrib
print "tag      ", xml.tag
print "text     ", xml.text
print "contents "
for element in xml :
    print element
print "tostring"
print ET.tostring( xml )

when run, produces:

<Element xml at 7f582c2e82d8>
attrib    {}
tag       xml
text

contents
<Element purchase at 7f582c2e8320>
<Element customer at 7f582c2e85a8>
tostring
<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>ju...@shrimp.edu</email>
        </customer>
</xml>

Which seems to me quite useful (i.e. it has the full XML available).
Maybe you can explain how you were trying to "play with" the results
of fromstring() that you can't do from parse().

The documentation for elementtree indicates:

> The ElementTree wrapper type adds code to load XML files as trees
> of Element objects, and save them back again.

and

> The Element type can be used to represent XML files in memory.
> The ElementTree wrapper class is used to read and write XML files.

In the above case, you should find that the getroot() of your loaded
ElementTree instance ( parse().getroot() ) to be the same as the
Element generated by fromstring().
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to