简体   繁体   English

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

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

I have an XML document through which I want ti retrieve the value of specific attribute using e tree. 我有一个XML文档,我希望通过它使用e树检索特定属性的值。 Below is the document-snippet I have: 以下是我的文档片段:

-<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>

I want to retrieve the value of only 'path' I have tried so far: 我想检索到目前为止我只尝试过的“路径”的值:

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

What should I do to only get the value of 'path' 我应该怎么做才能只获得“路径”的价值

You can use the Element class' find method with the path to the element you wish to pick out, for example: 您可以将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