Say i have this sample XML.
<result>
<field k='field1'>
<value h='1'><text>text_value1</text></value>
</field>
<field k='field2'>
<value><text>text_value2</text></value>
</field>
<field k='field3'>
<value><text>some_text</text></value>
</field>
</result>
Using python's lxml, how can i get the value of each field for every result set? So basically, i want to iterate over ever result set, then iterate over every field in that result set and print the text data.
This is what i have so far:
context = etree.iterparse(contentBuffer, tag='result')
for action, elem in context:
print elem.tag, elem.data
Any help would be greatly appreciated.
EDIT
Here is the code that i came up with. It seems a bit clunky having to call getparent() twice to read the attribute of corresponding text value. Is there a better way to do this?
for action, elem in context:
list = elem.xpath('//text')
print "result set:"
for item in list:
field = item.getparent().getparent().attrib['k']
value = item.text
print "%s = %s"%(field, value)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…