我如何使用 ElementTree 在 xml 文件中搜索标签,其中我有一个具有特定值的 "Parent" 标签? (python)

How do I search for a Tag in xml file using ElementTree where i have a certain "Parent"tag with a specific value? (python)

我刚开始学习 Python 并且必须编写一个程序来解析 xml 个文件。我必须在 2 个不同的文件中找到一个名为 OrganisationReference 的标签,然后 return 它。事实上,有多个具有此名称的标签,但只有一个,即我正在尝试 return 的标签,它具有值为 DEALER 的标签组织类型作为父标签(不太确定该术语是否正确)。为此,我尝试使用 ElementTree。这是代码:

    import xml.etree.ElementTree as ET

    tree1 = ET.parse('Master1.xml')
    root1 = tree1.getroot()

    tree2 = ET.parse('Master2.xml')
    root2 = tree2.getroot()

    for OrganisationReference in root1.findall("./Organisation/OrganisationId/[@OrganisationType='DEALER']/OrganisationReference"):
        print(OrganisationReference.attrib)

    for OrganisationReference in root2.findall("./Organisation/OrganisationId/[@OrganisationType='DEALER']/OrganisationReference"):
        print(OrganisationReference.attrib)

但这return没什么(也没有错误)。有人可以帮助我吗?

我的文件如下所示:

  <MessageOrganisationCount>a</MessageOrganisationCount>
  <MessageVehicleCount>x</MessageVehicleCount>
  <MessageCreditLineCount>y</MessageCreditLineCount>
  <MessagePlanCount>z</MessagePlanCount>
  <OrganisationData>
      <Organisation>
          <OrganisationId>
              <OrganisationType>DEALER</OrganisationType>
              <OrganisationReference>WHATINEED</OrganisationReference>
          </OrganisationId>
          <OrganisationName>XYZ.</OrganisationName>
 ....

由于 OrganisationReference 在这个文件中出现了几次,开始和结束标签之间的文本不同,我想得到你在第 9 行看到的那个:它有 OrganisationId 作为父标签, DEALER 也是 OrganisationId 的子标签。

您可以使用嵌套的 for 循环来完成它。首先检查 OrganisationType 的文本是否为 DEALER,然后获取您需要的 OrganisationReference 的文本。

如果您想了解更多关于使用 Python 解析 XML 的信息,我强烈推荐 XML 树库的 documentation

import xml.etree.ElementTree as ET

tree1 = ET.parse('Master1.xml')
root1 = tree1.getroot()

tree2 = ET.parse('Master2.xml')
root2 = tree2.getroot()

#Find the parent Dealer
for element in root1.findall('./Organisation/OrganisationId'):
    if element[0].text == "DEALER":
         print(element[1].text)

如果您的 OrganisationId 中的第一个标签是 OrganisationType,则此方法有效:)

您与最初的尝试非常接近。您只需要对您的 xpath 进行一些更改,并对您的 python.

进行微小的更改

xpath 的第一部分以 ./Organization 开头。由于您是从根执行 xpath,因此它期望 Organization 为 child。它不是;这是一个后代。

尝试将 ./Organization 更改为 .//Organization。 (///descendant-or-self::node()/ 的缩写。See here for more info.

第二个问题是 OrganisationId/[@OrganisationType='DEALER']。那是无效的 xpath。 / 应该从 OrganisationIdpredicate 之间删除。

此外,@attribute:: axis 的缩写语法,OrganisationType 是元素,而不是属性。

尝试将 OrganisationId/[@OrganisationType='DEALER'] 更改为 OrganisationId[OrganisationType='DEALER']

python 问题与 print(OrganisationReference.attrib) 有关。 OrganisationReference 没有任何属性;只是文字。

尝试将 print(OrganisationReference.attrib) 更改为 print(OrganisationReference.text)

这是一个仅使用一个 XML 文件进行演示的示例...

XML 输入(Master1.xml;加上 doc 元素使其成为 well-formed)

<doc>
    <MessageOrganisationCount>a</MessageOrganisationCount>
    <MessageVehicleCount>x</MessageVehicleCount>
    <MessageCreditLineCount>y</MessageCreditLineCount>
    <MessagePlanCount>z</MessagePlanCount>
    <OrganisationData>
        <Organisation>
            <OrganisationId>
                <OrganisationType>DEALER</OrganisationType>
                <OrganisationReference>WHATINEED</OrganisationReference>
            </OrganisationId>
            <OrganisationName>XYZ.</OrganisationName>
        </Organisation>
    </OrganisationData>
</doc>

Python

import xml.etree.ElementTree as ET

tree1 = ET.parse('Master1.xml')
root1 = tree1.getroot()

for OrganisationReference in root1.findall(".//Organisation/OrganisationId[OrganisationType='DEALER']/OrganisationReference"):
    print(OrganisationReference.text)

打印输出

WHATINEED

另请注意,您似乎根本不需要使用 getroot()。可以直接在树上使用findall()...

import xml.etree.ElementTree as ET

tree1 = ET.parse('Master1.xml')

for OrganisationReference in tree1.findall(".//Organisation/OrganisationId[OrganisationType='DEALER']/OrganisationReference"):
    print(OrganisationReference.text)