XML - mwicat/personal GitHub Wiki

XPath from command line

sudo apt  install xmlstarlet
xmlstarlet select --text --template --match '//option' --value-of '@title' --nl file.xml

ElementTree

import xml.etree.ElementTree as ET

root = ET.fromstring(countrydata)
nodes = root.findall(".//country/neighbor")
for node in nodes:
    print(node.text)
    print(node.attrib['key'])

XQuery

from pyquery import PyQuery as pq
d = pq(open('xml/a_team.xml').read())
notes = [(note.get('name'), note.get('duration')) for note in d("note")]
print notes