ElementTree handling nested tag
Diez B. Roggisch
deets at web.de
Sun Oct 3 14:09:22 EDT 2010
tekion <tekion at gmail.com> writes:
> On Oct 2, 5:32 am, de... at web.de (Diez B. Roggisch) wrote:
>> tekion <tek... 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
>
> Diez,
> This is the sample format from the doc. I the whole log file has this
> xml formated beginning and ending in the event tag. Is this something
> ElemenTtree can handle or is it better to user XSLT? Thanks.
Handle *what*? Can it read it? Yes. Can it extract data from it?
Yes. You still haven't said what you actually *want* with all this.
Diez
More information about the Python-list
mailing list