Reputation: 341
I have a XML string as the following:
<?xml version="1.0" encoding="UTF-8" ?>\n<data serial="1234">\n <ts>1418823725</ts>\n <r t="P" n="AA"><v>1596787</v><i>62</i></r>\n <r t="P" n="BB"><v>246995</v><i>63</i></r>\n <r t="P" n="CC"><v>0</v><i>0</i></r>\n</data>\n
I am trying to parse the string to get the this using xml library in python as follows:
for child in root.iter('r'):
print child.attrib.get('t')
print child.attrib.get('n')
print child.text
output shows as:
P
AA
None
P
BB
None
P
CC
None
How can I access time stamp value <ts>
, the number 62
63
and 0
for AA
BB
and CC
?
Upvotes: 0
Views: 30
Reputation: 474161
findtext()
would be handy here:
print root.findtext('ts')
print "----"
for child in root.iter('r'):
print child.attrib.get('t'), child.attrib.get('n'), child.findtext('i')
Prints:
1418823725
----
P AA 62
P BB 63
P CC 0
Upvotes: 2