ElementTree handling nested tag

tekion tekion at gmail.com
Mon Oct 4 02:41:54 CEST 2010


On Oct 3, 2:09 pm, de... at web.de (Diez B. Roggisch) wrote:
> tekion <tek... 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

I wan to get the value of these tags:

    <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
    <httpmethod>GET</httpmethod>
    <httpresponse>200</httpresponse>

You asked for what the upper layer tags are.  The upper layer tags I
am referring for below tags are any tag that is above it.

    <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
    <httpmethod>GET</httpmethod>
    <httpresponse>200</httpresponse>

IE, for the upper upper are: <action>httpRequest</action> and <event>
tags.




More information about the Python-list mailing list