我刚开始学习 Python,必须编写一个程序来解析 xml 文件。我必须在 2 个不同的文件中找到一个名为 OrganisationReference 的标签并将其返回。事实上,有多个具有此名称的标签,但只有一个,即我要返回的标签,它具有值为 DEALER 的标签 OrganisationType 作为父标签(不太确定该术语是否正确)。为此,我尝试使用 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)
但这不会返回任何内容(也没有错误)。有人可以帮助我吗?
我的文件看起来像这样:
<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 的子标签。
最佳答案
您与最初的尝试非常接近。您只需要对您的 xpath 进行一些更改,并对您的 python 进行微小的更改。
xpath 的第一部分以 ./Organization
开头。由于您是从根执行 xpath,因此它期望 Organization
是一个 child 。不是;这是一个后代。
尝试将 ./Organization
更改为 .//Organization
。 (//
是 /descendant-or-self::node()/
的缩写。See here for more info. )
第二个问题与 OrganisationId/[@OrganisationType='DEALER']
有关。那是无效的 xpath。 /
应该从 OrganisationId
和 predicate 之间删除.
此外,@
是 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
元素以使其格式正确)
<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)
关于python - 如何使用 ElementTree 在 xml 文件中搜索标签,其中我有一个具有特定值的特定 "Parent"标签? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361331/