Python Xml Parsing With Tag Specific Info
I have an XML file with a structure like the following: ver1 content
Solution 1:
You can access an element with a fine xpath expression :
XPath explained
//
stands for relative & recursive search
'//ch[@num="1"][@bk="Book1"]/ver[@num="1"]'
# ^ ^ ^ ^ ^# ch node | | | |# + attributes num = 1 | |# + AND Book attribute = 1 | |# ver node |# + num attribut = 1
python code :
from lxml import etree
fp = open("/tmp/xml.xml")
tree = etree.parse(fp)
print(tree.xpath('//ch[@num="1"][@bk="Book1"]/ver[@num="1"]/text()')[0])
Solution 2:
A simple approach using the Python standard library's xml.etree.ElementTree
:
import xml.etree.ElementTree as ET
tree = ET.parse('yourfile.xml')
def locate(chnum, bk, vernum):
for ch in tree.findall('ch'):
if ch.get('num') != chnum: continueif ch.get('bk') != bk: continuefor ver in ch.findall('ver'):
if ver.get('num') != vernum: continuereturn ver.text
return None # no such chapter/book/version combo found
Solution 3:
Yes, you can use xpath to get target tag.
>>>from lxml import etree>>>fp = open("test.html")>>>tree = etree.parse(fp)>>>r = tree.xpath('//ch[@num=2][@bk="Book1"]/ver/text()')>>>r
['ver1 content', 'ver2 content']
Solution 4:
This way, you can access ver
as elements.
import xml.etree.ElementTree as etree
tree = etree.ElementTree(file='input.xml')
#inputs
num = '1'
bk = 'Book2'#list comprehension (assume num and bk is unique for ch)
vers = [ch.findall('ver') \
for ch in tree.findall('ch') \
if ch.attrib['num'] == num and ch.attrib['bk'] == bk][0]
#loop resultsfor ver in vers:
print'num={0} text={1}'.format(ver.attrib['num'], ver.text)
Post a Comment for "Python Xml Parsing With Tag Specific Info"