[英]Xml in python with unbalanced/uneven elements/tags
I have a an xml file with uneven/unbalanced elements/fields, it means there is </>
but not <>
.我有一个包含不均匀/不平衡元素/字段的 xml 文件,这意味着有
</>
但没有<>
。 For example (for simplicity, only copied part of the xml file):例如(为简单起见,只复制了 xml 文件的一部分):
<myTag>
text1
text2
<no_open/>
text3
text4
</myTag>
Now, I want to have a python programs which reads this xml file and print the tag values as follow:现在,我想要一个 python 程序来读取这个 xml 文件并打印如下标签值:
text1
text2
text3
text4
However, because of this uneven element然而,由于这种不均匀的元素
<no_open/>
it only prints the following and ignore the rests:它只打印以下内容并忽略其余部分:
text1
text2
Now, what should be the solution if I want my python ignore the no_open and prints the desired output.现在,如果我想让我的 python 忽略no_open并打印所需的输出,那么解决方案应该是什么。 Any help would be appreciated.
任何帮助,将不胜感激。
Update:更新:
Here is my code:这是我的代码:
with open('test.xml', "r") as fp:
tree = ElementTree.parse(fp)
root = tree.getroot()
release_data = root[0].text
for tag in root.iter('tag0'):
for c in tag:
print c.text
and test.xml is:和 test.xml 是:
<tag0>
<myTag>
text1
text2
<no_open/>
text3
text4
</myTag>
</tag0>
You can try this way :你可以试试这种方式:
tree = ElementTree.parse(fp)
root = tree.getroot()
target_tag = root.find('myTag')
#collect all text nodes in <myTag> and join
result = ''.join(target_tag.itertext())
print(result)
output :输出 :
text1
text2
text3
text4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.