2

Is there a quick way to take this block of XML and extract the value of "version"?

   <xml>
   <creator version='1.0'>
     <program>BULK_EXTRACTOR</program>
     <version>1.0.3</version>
     <build_environment>
       <compiler>GCC 4.2</compiler>
       <compilation_date>2011-09-27T11:56:35</compilation_date>
       <library name="afflib" version="3.6.12"></library>
       <library name="libewf" version="20100226"></library>
     </build_environment>
    </creator>
    </xml>

I know that I can do this with Python's Beautiful Soup, but I'm look for a simple way to do it with the DOM.

Thanks!

vy32
  • 28,461
  • 37
  • 122
  • 246

2 Answers2

4

Assuming you are looking for the version element, not the version attributes, using lxml:

import lxml.etree as ET

content='''\
   <xml>
   <creator version='1.0'>
     <program>BULK_EXTRACTOR</program>
     <version>1.0.3</version>
     <build_environment>
       <compiler>GCC 4.2</compiler>
       <compilation_date>2011-09-27T11:56:35</compilation_date>
       <library name="afflib" version="3.6.12"></library>
       <library name="libewf" version="20100226"></library>
     </build_environment>
    </creator>
    </xml>
'''

doc=ET.fromstring(content)
version=doc.xpath('creator/version/text()')[0]
print(version)
# 1.0.3

To find the version attributes:

for elt in doc.xpath('//*[@version]'):
    print(elt.tag, elt.attrib.get('name'), elt.attrib.get('version'))
# ('creator', None, '1.0')
# ('library', 'afflib', '3.6.12')
# ('library', 'libewf', '20100226')
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
2

If you don't have lxml installed, you can use ElementTree which is included in the standard library:

>>> import xml.etree.ElementTree
>>> doc = xml.etree.ElementTree.fromstring(content)
>>> doc.findtext('creator/version')
'1.0.3'
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485