Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Lxml (objectify): Checking whether a tag exists

Tags:

I need to check whether a certain tag exists in an xml file.

For example, I want to see if the tag exists in this snippet:

 <main>        <elem1/>        <elem2>Hi</elem2>        <elem3/>        ...  </main> 

Currently, I am using an ugly hack with error checking, like this:

try:    if root.elem1.tag:       foo = elem1 except AttributeError:    foo = "error finding elem1" 

I also want to customize the string if it is unable to find the node (i.e. "unable to find -tagname-").

I have to check a long list of variables, and I don't want to repeat the code 100 times.

Any suggestions?

Edit:

Here is a snip of the actual xml file:

<main>  <asset name="Virtual Dvaered Unpresence">   <virtual/>   <presence>    <faction>Dvaered</faction>    <value>-1000.000000</value>    <range>0</range>   </presence>  </asset>  <asset name="Virtual Empire Small">   <virtual/>   <presence>    <faction>Empire</faction>    <value>100.000000</value>    <range>2</range>   </presence>  </asset> </main> 

I want to check whether the tag exists, and, if so, to get the contents.

Edit edit: Ok, I am going to combine two of the answers, but I can only vote for one. Sorry.

Edit 3: Related question about XPath here: Python lxml (objectify): Xpath troubles

like image 807
Biosci3c Avatar asked Mar 22 '11 01:03

Biosci3c


2 Answers

hasattr() works for this:

if hasattr(root, 'elem1'):     foo = root.elem1 
like image 128
Sleep_Walker Avatar answered Sep 19 '22 02:09

Sleep_Walker


Edit: updated answer for sample file.

I'm assuming you want to search each asset for certain tags. If so, the following worked for me:

import lxml.objectify  # Parse the file. tree = lxml.objectify.parse('sample.xml') root = tree.getroot()  # Which elements to find. to_find = set(['presence/faction', 'presence/value', 'fake'])  # Go through each asset in the document. for asset in root.findall('asset'):     # Check for each element.      for name in to_find:         node = asset.find(name)         if node is not None:             print 'Found %s, its value is %s' % (name, node)         else:             print 'Unable to find %s' % name 

The output was:

Found presence/value, its value is -1000.0 Found presence/faction, its value is Dvaered Unable to find fake Found presence/value, its value is 100.0 Found presence/faction, its value is Empire Unable to find fake 
like image 36
Blair Avatar answered Sep 18 '22 02:09

Blair