喜欢用lxtrouplee搜索吗

2024-04-25 22:25:08 发布

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

假设我有以下xml:

<?xml version="1.0" encoding="utf-8"?>
<FeedType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://foo.com/bar" xsi:schemaLocation="https://foo.com/bar https://foo.com/bar/arr.xsd" value="Type">
    <ElementName value='Type'>
        <DataIWant>
            stuff
        </DataIWant>
        <DataIWant>
            other stuff
        </DataIWant>
    </ElementName>
</FeedType>

我想得到ElementName标记中的所有内容。

在美丽的人群中,人们可以打电话

^{pr2}$

它将返回一个以ElementName为根的树。

如何在lxml中执行此操作?


Tags: httpsorgcomhttpfoowwwbarxml
1条回答
网友
1楼 · 发布于 2024-04-25 22:25:08

lxml有一个^{} method,可以使用它。在

但是,XML文档包含一个默认的命名空间,因此搜索普通的ElementName标记将找不到它-您需要指定名称空间:

root.findall('foobar:ElementName', namespaces = {'foobar': 'https://foo.com/bar'})

如果不想指定名称空间,可以使用XPath查询来忽略名称空间,只查找“local name”为ElementName的元素:

^{pr2}$

相关问题 更多 >