从 xml 文件中提取元素

问题描述 投票:0回答:1

我有以下 xml 文件:

<components version="1.0.0">
    <component type="foo">
        <sample>Foo<sample>
        <sample lang=a>abc</sample>
        <sample lang=b>efj</sample>
    </component>
</components>
from lxml import etree

def parse(path: str):

    return etree.parse(path)

def components(path: str) -> list:

    components = parse_appinfo_xml(path).xpath("/components/component")
    return list(components)

def sample(path: str) -> str:
    sample = components(path)[0].find("sample").text

    return str(sample)

path = "test.xml"
print(sample(path))

我想遍历所有

sample
标签并获取没有
lang
属性的标签的值,即。第一个。我该怎么做呢?我知道我需要使用 for 循环,但不确定如何检查
lang
是否存在。

python lxml
1个回答
0
投票

您可以检查

lang
是否不在标签的属性中:

from lxml import etree

xml_string = """
<components version="1.0.0">
    <component type="foo">
        <sample>Foo</sample>
        <sample lang="a">abc</sample>
        <sample lang="b">efj</sample>
    </component>
</components>
"""

root = etree.fromstring(xml_string)

for sample in root.findall("component/sample"):
    if "lang" not in sample.attrib:
        print(sample.text)

打印:

Foo
© www.soinside.com 2019 - 2024. All rights reserved.