繁体   English   中英

使用E树检索特定属性的值

[英]Retrieving the value of specific attribute using E Tree

我有一个XML文档,我希望通过它使用e树检索特定属性的值。 以下是我的文档片段:

-<orcid-message>
    <message-version>1.2</message-version>
  -<orcid-profile type="user">
    -<orcid-identifier>
        <uri>http://orcid.org/0000-0001-5105-9000</uri>
        <path>0000-0001-5105-9000</path>

我想检索到目前为止我只尝试过的“路径”的值:

tree = ET.parse(file)
root = tree.getroot()
for element in root:
    for all_tags in element.findall('.//'):
         if all_tags.text:
             print all_tags.text, '|', all_tags.tail

我应该怎么做才能只获得“路径”的价值

您可以将Element类的find方法与要提取的元素的路径一起使用,例如:

import xml.etree.ElementTree as ET

tree = ET.parse("filename")
root = tree.getroot()
path = root.find("orcid-profile/orcid-identifier/path")
print(path.text)

暂无
暂无

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

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