如何使用python查找xml中特定标记elemnet中的值?

2024-06-09 05:03:45 发布

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

我试图解析从RESTful接口接收到的xml数据。在错误情况下(当查询在服务器上没有结果时),将返回以下文本。现在,我想解析这个字符串来搜索下面给出的示例中第五行中的status的值。我怎样才能找到状态是否存在,如果存在,那么它的价值是什么。在

content = """
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/3.0/style/exchange.xsl"?>
<ops:world-patent-data xmlns="http://www.epo.org/exchange" xmlns:ops="http://ops.epo.org" xmlns:xlink="http://www.w3.org/1999/xlink">
    <ops:meta name="elapsed-time" value="3"/>
    <exchange-documents>
        <exchange-document system="ops.epo.org" country="US" doc-number="20060159695" status="not found">
            <bibliographic-data>
                <publication-reference>
                    <document-id document-id-type="epodoc">
                        <doc-number>US20060159695</doc-number>
                    </document-id>
                </publication-reference>
                <parties/>
            </bibliographic-data>
        </exchange-document>
    </exchange-documents>
</ops:world-patent-data>
"""
import xml.etree.ElementTree as ET
root = ET.fromstring(content)
res = root.iterfind(".//{http://www.epo.org/exchange}exchange-documents[@status='not found']/..")

Tags: orgidhttpnumberdatadocexchangewww
2条回答

只需使用BeautifulSoup:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(open('xml.txt', 'r'))

print soup.findAll('exchange-document')["status"]

#> not found 

如果将每个xml输出存储在一个文件中,则对它们进行迭代会很有用:

^{pr2}$

这将显示[exchange document]元素中的每个[status]标记。在

另外,如果您只想要有用的状态,您应该:

for tag in soup.findAll('exchange-document'):
    if tag["status"] not in "not found":
        print tag["status"]

试试这个:

from xml.dom.minidom import parse
xmldoc = parse(filename)
elementList = xmldoc.getElementsByTagName(tagName)

elementList将包含具有指定标记名的所有元素,然后您可以迭代这些元素。在

相关问题 更多 >