ElementTree handling nested tag

tekion tekion at gmail.com
Sun Oct 3 17:36:00 CEST 2010


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.
<event rev="1.2">
  <date>2005-10-02-22:01:36.187-04:00I-----</date>
  <outcome status="953091111" reason="unauthorized">1</outcome>
  <originator blade="webseald" instance="default">
    <component rev="1.2">http</component>
    <event_id>109</event_id>
    <action>1</action>
    <location>cmd.wma.ibm.com</location>
  </originator>
  <accessor name="unauthenticated">
    <principal auth="IV_UNAUTH_V3.0" domain="Default">Unauthenticated</
principal>
    <user_location>9.54.83.206</user_location>
    <user_location_type>IPV4</user_location_type>
  </accessor>
  <target resource="5">
    <object>/</object>
    <object_nameinapp>HTTP://cmd.wma.ibm.com:80/</object_nameinapp>
  </target>
  <resource_access>
    <action>httpRequest</action>
    <httpurl>HTTP://cmd.wma.ibm.com:80/</httpurl>
    <httpmethod>GET</httpmethod>
    <httpresponse>200</httpresponse>
  </resource_access>
  <data>
    GET HTTP://cmd.wma.ibm.com:80/ HTTP/1.0
    1970
    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  </data>
</event>




More information about the Python-list mailing list