简体   繁体   English

有没有办法根据 python 中的特定路径从 XML 返回标签的值?

[英]Is there a way to return the value for a tag from a XML based on a specific path in python?

I have this XML我有这个 XML

    <Body>
    <Batch_Number>2000</Batch_Number>
    <Total_No_Of_Batches>12312</Total_No_Of_Batches>
    <requestNo>1923</requestNo>
    <Parent1>
        <Parent2>
            <Parent3>
                    <lastModifiedDateTime>2022-11-11T11:07:30.000</lastModifiedDateTime>
                    <purpose>NeverMore</purpose>
                    <endDate>9999-12-31T00:00:00.000</endDate>
                    <createdDateTime>2019-06-06T06:32:16.000</createdDateTime>
                    <createdOn>2019-06-06T08:32:16.000</createdOn>
                    <address2>Forever street 21</address2>
                    <externalCode>code123</externalCode>
                    <lastModifiedBy>user2.thisUser</lastModifiedBy>
                    <lastModifiedOn>2039-06-11T13:07:30.000</lastModifiedOn>
                    <lastModifiedBy>MG</lastModifiedBy>
                    <PS>1234431</PS>
            </Parent3>
        </Parent2>
    </Parent1>
</Body>

Is there a way to return the value for lastModifiedBy for example where the path has this specific structure:有没有办法返回 lastModifiedBy 的值,例如路径具有以下特定结构:

Body.Parent1.Parent2.Parent3.lastModifiedBy

Idealy, I would like to populate a dictionary with the child tag name and its value, for example:理想情况下,我想用子标签名称及其值填充字典,例如:

dict[lastModifiedBy.tag] = lastModifiedBy.text

You can use xml from standart library for working with xml files.您可以使用标准库中的xml来处理 xml 文件。

from xml.etree import ElementTree as ET

tree = ET.parse("d.xml") # our xml file
root = tree.getroot()

And then you can access elements as indexes or you can use root like as a list:然后您可以将元素作为索引访问,也可以将root用作列表:

for i in root:
    print(i)

A XML element may have more than one child with same tag name (even you have two lastModifiedBy in the Parent3 ).一个 XML 元素可能有多个具有相同标签名称的子元素(即使您在Parent3中有两个lastModifiedBy )。 This is why we use them like lists, they works like a list.这就是为什么我们像列表一样使用它们,它们像列表一样工作。 So you shouldn't try to use them like dictionary.所以你不应该尝试像字典一样使用它们。

I think you need to use XPath .我认为您需要使用XPath Like so:像这样:

from xml.etree import ElementTree as ET

tree = ET.parse("d.xml") # our xml file
root = tree.getroot()


s = root.findall(".Parent1/Parent2/Parent3/lastModifiedBy")

for i in s:
    print(i.text)

This gives you all lastModifiedBy elements in the Parent3 element.这为您提供了Parent3元素中的所有lastModifiedBy元素。 You can access to any index if you want too, like this:如果您愿意,您也可以访问任何索引,如下所示:

from xml.etree import ElementTree as ET

tree = ET.parse("d.xml") # our xml file
root = tree.getroot()


s = root.find(".Parent1/Parent2/Parent3/lastModifiedBy[1]") # first element with "lastModifiedBy" tag

print(s.text)

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

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