我正在寻找一种从非常大的 xml 文档中获取特定标签的方法
使用内置模块的 python dom
例如:
<AssetType longname="characters" shortname="chr" shortnames="chrs">
<type>
pub
</type>
<type>
geo
</type>
<type>
rig
</type>
</AssetType>
<AssetType longname="camera" shortname="cam" shortnames="cams">
<type>
cam1
</type>
<type>
cam2
</type>
<type>
cam4
</type>
</AssetType>
我想检索获得属性 (longname= "characters") 的 AssetType 节点的子节点的值
得到 'pub','geo','rig'
的结果
请记住我有超过 1000 个
提前谢谢
最佳答案
如果您不介意将整个文档加载到内存中:
from lxml import etree
data = etree.parse(fname)
result = [node.text.strip()
for node in data.xpath("//AssetType[@longname='characters']/type")]
您可能需要删除标签开头的空格才能使其正常工作。
关于python - 如何使用python获取xml文件中的特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2230677/