ElementTree handling nested tag

T

tekion

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.
 
D

Diez B. Roggisch

tekion said:
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
 
T

tekion

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>
 
D

Diez B. Roggisch

tekion said:
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
 
T

tekion

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.
 
D

Dennis Lee Bieber

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.

Based upon the structure you posted, <action> is NOT an "upper
level" tag -- it is a peer to the three you state you want to retrieve.

I'm no expert at XML or elementTree but...

You retrieve the <event> tag and from it extract its contents as a
node; from that node you retrieve the <resource...> tag and extract its
contents... From that node you can retrieve the three peer tags by
asking for them by name.
 
D

Diez B. Roggisch

tekion said:
I wan to get the value of these tags:

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


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)

for resource_access in doc.findall("resource_access"):
print resource_access.find("httpurl").text
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.

That's not true. action is on the same level as the others. And what you
you want to "get" from the upper layer tags?

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top