I'm trying to run Xpath queries using etree, but I can't seem to figure out the Xpath syntax.
Here's a (sanitized) section of my XML document.
Here's what I need to do:
1) Query using XML based on some value like the 'id' or 'displayName' and have access to all of the information (i.e. the Application, Properties, Links, etc. sections).
2) Query other values/sections. I assume that this will be obvious once I figure out how to do a basic query. What I'm thinking is the find the correct "Application," then explore the other siblings of that node.
3) This XML is actually fairly large, so I'd like to iterparse() instead of parse(), but I can't get that to work.
Here's what I've tried:
That successfully finds the node, but I can't figure out how to get any information out of it. The problem seems to be that etree.xpath() returns a list instead of a more complex object that has additional methods/attributes like attrib or get().
Could someone help me figure out what to do with the return from etree.xpath()?
Thanks.
Here's a (sanitized) section of my XML document.
This XML consists of hundreds of <Application>...</Application> sections.<?xml version="1.0"?>
<Applications>
<Application id="61-X"
displayName="61-X"
type="IHS"
webSiteName="61-X"
environment="X">
<Properties>
<Property name="uploadDir"
value=""/>
<Property name="contentDir"
value="/data01/opt/ihs/ihs61/htdocs/61-heritage-IHS61"/>
</Properties>
<Notification defaultEmail="firstname.lastname@example.com"/>
<Links>
<Link src="/data01/opt/websphere61/AppServer6/profiles/AppSrv01/installedApps/X/233-X.ear/X.war/WEB-INF/classes/application.properties_prod"
dest="/data01/opt/websphere61/AppServer6/profiles/AppSrv01/installedApps/X/233-X.ear/X.war/WEB-INF/classes/application.properties"
userId="was61"
host="X.Y.Z.net" />
<Link src="/data01/opt/websphere61/AppServer6/profiles/AppSrv01/installedApps/X/233-X.ear/X.war/WEB-INF/classes/application.properties_prod"
dest="/data01/opt/websphere61/AppServer6/profiles/AppSrv01/installedApps/X/233-X.ear/X.war/WEB-INF/classes/application.properties"
userId="was61"
host="X.Y.Z.net" />
<!-- DO NOT DELETE: 233-X-NA Link -->
</Links>
</Application>
</Applications>
Here's what I need to do:
1) Query using XML based on some value like the 'id' or 'displayName' and have access to all of the information (i.e. the Application, Properties, Links, etc. sections).
2) Query other values/sections. I assume that this will be obvious once I figure out how to do a basic query. What I'm thinking is the find the correct "Application," then explore the other siblings of that node.
3) This XML is actually fairly large, so I'd like to iterparse() instead of parse(), but I can't get that to work.
Here's what I've tried:
Code:
>>>from lxml import etree
>>>file = open('apps.xml')
>>>tree = etree.parse(file)
>>>test = tree.xpath('//Application[@id="61-X"]')
>>>print(test)
[<Element Application at 1bbf470>]
Could someone help me figure out what to do with the return from etree.xpath()?
Thanks.