简体   繁体   English

Python:获取特定XML标签的所有内容

[英]Python: get all content of specific XML tags

I have parsed a XML file and need to get all content from all <name> tags. 我已经解析了一个XML文件,需要从所有<name>标记中获取所有内容。

So far I've come up with this: 到目前为止,我已经提出了:

xml_tag = dom.getElementsByTagName('person')[0].toxml()
xml_data = xml_tag.replace('<name>','').replace('</name>','')

This however only gets the first tag. 但是,这仅获得第一个标签。 How do I create a list of all the name tags content in it? 如何创建其中所有名称标签内容的列表?

usually I use lxml for these things but looking at your code, this or somethin similar should work. 通常,我使用lxml进行这些操作,但查看您的代码,这个或类似的东西应该可以工作。

xml_tag = dom.getElementsByTagName('person')[0]
xml_data = [elem.nodeValue for elem in dom.getElementsByTagName('name')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM