简体   繁体   English

Python - 使用 ElementTree 使用重复标签解析 XML

[英]Python - Parse XML with repeated tags using ElementTree

I have the following XML content:我有以下 XML 内容:

<plist version="1.0">
<dict>
    <key>Version</key><integer>1</integer>
    <key>Sub Version</key><integer>2</integer>
    <dict>
        <key>1</key>
        <dict>
            <key>ID</key><integer>1</integer>
            <key>Name</key><string>Frank</string>
        </dict>
        <key>2</key>
        <dict>
            <key>ID</key><integer>2</integer>
            <key>Name</key><string>Richard</string>
        </dict>
        <key>3</key>
        <dict>
            <key>ID</key><integer>3</integer>
            <key>Name</key><string>Sophia</string>
        </dict>
    </dict>
    <key>Persons</key>
    <array>
        <dict>
            <key>Name</key><string>Persons</string>
            <key>Description</key><string>empty</string>
        </dict>
    </array>
</dict>
</plist>

I'm having a hard time retrieving the names since this XML tags names are all the same and have no attributes.我很难检索名称,因为这个 XML 标签名称都是相同的并且没有属性。 So far I've tried to access it using iteration over the "second depth dict" but I can't retrieve just what I want.到目前为止,我已经尝试使用“第二深度字典”上的迭代来访问它,但我无法检索到我想要的内容。

What I got:我得到了什么:

from xml.etree import ElementTree as et

tree = et.parse("file.xml")
root = tree.getroot()

for i in root.find('dict').find('dict').iter('dict'):
    print ([j.text for j in i])

The output I want:我想要的 output:

Frank
Richard
Sophia

Does anyone know how to access these values with such tags?有谁知道如何使用这些标签访问这些值?

Try it using lxml instead:改用 lxml 试试:

from lxml import etree
plist = """your xml above"""

doc = etree.fromstring(plist)
doc.xpath('//dict/dict/key["name"]/following-sibling::string/text()')

output: output:

['Frank', 'Richard', 'Sophia']

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

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