[英]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.