我试图解析从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']/..")
只需使用BeautifulSoup:
如果将每个xml输出存储在一个文件中,则对它们进行迭代会很有用:
^{pr2}$这将显示[exchange document]元素中的每个[status]标记。在
另外,如果您只想要有用的状态,您应该:
试试这个:
elementList
将包含具有指定标记名的所有元素,然后您可以迭代这些元素。在相关问题 更多 >
编程相关推荐