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

2024-04-26 03:39:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学习Python,必须编写一个程序来解析xml文件。我必须在两个不同的文件中找到一个名为organizationreference的标记并返回它。事实上,有多个具有此名称的标记,但只有一个标记organizationType和value 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)

但这不会返回任何结果(也不会返回错误)。有人能帮帮我吗?在

我的文件如下:

^{pr2}$

由于organizationreference在这个文件中多次出现,在start和endtag之间有不同的文本,所以我想得到第9行中看到的那个:它有organizationid作为父标记,DEALER也是organizationid的子标记。在


Tags: 文件标记forparsexmletelementtreedealer
2条回答

可以使用嵌套的for循环来执行此操作。首先检查OrganisationType的文本是否为DEALER,然后获得所需的OrganisationReference的文本。在

如果您想了解使用Python解析XML的更多信息,我强烈推荐XMLtree库的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)

如果organizationId中的第一个标记是organizationtype:)

你和你最初的尝试非常接近。您只需要对xpath和python进行一些更改。在

xpath的第一部分以./Organization开头。因为您是从根执行xpath,所以它期望Organization是一个子级。不是,它是后代。在

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

第二个问题是OrganisationId/[@OrganisationType='DEALER']。这是无效的xpath。/应该从OrganisationId和{a2}之间删除。在

另外,@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

^{pr2}$

打印输出

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)

相关问题 更多 >

    热门问题