8

I was using MINIDOM but it does not provide xpath methods.

I am now trying to use libxml2 but I am having trouble retrieving attribute values.

An extract of my xml looks as follow:

<Class name="myclass1" version="0">
    <Owner user-login="smagnoni"/>
</Class>

and I wrote the following code:

import libxml2
doc = libxml2.parseFile(file)
ris = doc.xpathEval('*/Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
print str(ris[0])

which returns:

<Owner user-login="smagnoni"/>

How do I get just "smagnoni"? Parsing the string by hand feels overworked. but I did not find a method comparable to .getAttribute("attribute-name") in minidom.

Can anyone suggest the proper method or direct me to documentation?

3
  • 1
    I can't find this in their (seemingly lacking) documentation either. It might possibly have a .get method. You could also consider using the 'industry standard' lxml library. Commented Jul 22, 2011 at 14:39
  • lxml uses libxml2 and provides a nicer interface (the ElementTree api). Commented Jul 22, 2011 at 14:45
  • ikanobori is right, don't use the default libxml2 python bindings, use lxml, which is a much better interface to the same library. Commented Jul 22, 2011 at 14:47

3 Answers 3

5

.prop('user-login') should work:

import libxml2
import io
content='''\
<Class name="myclass1" version="0">
    <Owner user-login="smagnoni"/>
</Class>
'''
doc = libxml2.parseMemory(content,len(content))
className='myclass1'
classVersion='0'
ris = doc.xpathEval('//Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')

elt=ris[0]
print(elt.prop('user-login'))

yields

smagnoni
Sign up to request clarification or add additional context in comments.

2 Comments

by the way... do you have any link for a reference documentation?? i am really trying to guess how to use this library. I found information for c implementation but for python not so much. Even if it claims to be a stable and really good library i think it lacks in documentation. Cheers, ste
@stefano: I don't know of any particularly good documentation -- I mainly use lxml. I was able to guess my way to an answer here by fiddling around with code in IPython -- Typing ris.<TAB> shows you all the attributes of ris. Poking around like this led me to elt.prop.
4
for owner in ris:
    for property in owner.properties:
        if property.type == 'attribute':
            print property.name
            print property.content

2 Comments

good... in this case it was easyer to use the .prop() but since in other case i may have multiple results your foor loop may help. Tnx for the help
ouch... i get an error doing so: "for property in ris.properties: AttributeError: 'list' object has no attribute 'properties'" :(
2

lxml uses libxml2 and provides a nicer interface (the ElementTree api) so you get most of the benefit of libxml2's speed and all of the benefit of it's xpath evaluation.

import lxml.etree as ET

doc = ET.parse(file)
owner = doc.find('/*/Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
if owner:
    print owner.get('user-login')

The added bonus is that the Element Tree api is available by default in python2.5 (though the version in 1.5 does not include the [@name='value'] xpath syntax, that was added in python 2.7, but you can get the 1.3 api as a separate package in older 2.x versions of python).

You can import any compatible version of the ElementTree api using:

try:
  from lxml import etree
  print("running with lxml.etree")
except ImportError:
  try:
    # Python 2.5
    import xml.etree.cElementTree as etree
    print("running with cElementTree on Python 2.5+")
  except ImportError:
    try:
      # Python 2.5
      import xml.etree.ElementTree as etree
      print("running with ElementTree on Python 2.5+")
    except ImportError:
      try:
        # normal cElementTree install
        import cElementTree as etree
        print("running with cElementTree")
      except ImportError:
        try:
          # normal ElementTree install
          import elementtree.ElementTree as etree
          print("running with ElementTree")
        except ImportError:
          print("Failed to import ElementTree from any known place")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.