Xml in python with unbalanced/uneven elements/tags -
i have xml file uneven/unbalanced elements/fields, means there </>
not <>
. example (for simplicity, copied part of xml file):
<mytag> text1 text2 <no_open/> text3 text4 </mytag>
now, want have python programs reads xml file , print tag values follow:
text1 text2 text3 text4
however, because of uneven element
<no_open/>
it prints following , ignore rests:
text1 text2
now, should solution if want python ignore no_open , prints desired output. appreciated.
update:
here code:
open('test.xml', "r") fp: tree = elementtree.parse(fp) root = tree.getroot() release_data = root[0].text tag in root.iter('tag0'): c in tag: print c.text
and test.xml is:
<tag0> <mytag> text1 text2 <no_open/> text3 text4 </mytag> </tag0>
you can try way :
tree = elementtree.parse(fp) root = tree.getroot() target_tag = root.find('mytag') #collect text nodes in <mytag> , join result = ''.join(target_tag.itertext()) print(result)
output :
text1 text2 text3 text4
Comments
Post a Comment