ElementTree handling nested tag

Diez B. Roggisch deets at web.de
Sat Oct 2 05:32:33 EDT 2010


tekion <tekion at gmail.com> writes:

> All,
> I have the following xml tag:
> <event>
> <resource_access>
>       <action>httpRequest</action>
>       <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
>       <httpmethod>GET</httpmethod>
>       <httpresponse>200</httpresponse>
>    </resource_access>
> </event>
>
> I am interested in:
>        <action>httpRequest</action>
>       <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
>       <httpmethod>GET</httpmethod>
>       <httpresponse>200</httpresponse>
> as well as the upper layer tag. How do I get at the nest tag listed
> above?  Thanks.

What is the "upper layer tag"? And what do you actually want to "get"?
The text-values? Or do you want to just create a document that just
consists of the resource_access tag?

Then this should help:


from xml.etree.ElementTree import *

doc = """
<event>
<resource_access>
      <action>httpRequest</action>
      <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
      <httpmethod>GET</httpmethod>
      <httpresponse>200</httpresponse>
   </resource_access>
</event>
"""


doc = fromstring(doc)

resource_access = doc.find("resource_access")
print tostring(resource_access)

Diez




More information about the Python-list mailing list