简体   繁体   English

在 Python 中搜索 XML 内容

[英]Search XML content in Python

I have the following XML file:我有以下 XML 文件:

With some help of StackOverflow, I managed to achieve some of what I planned to do.StackOverflow 的帮助下,我设法实现了我计划做的一些事情。 Now, I want to add a search function to my script and singularly work on that sub-tree.现在,我想在我的脚本中添加一个搜索功能并单独处理该子树。 For example, I ask the user - what ID?比如我问用户——什么ID? He enters AL2012-2015-088 .他进入AL2012-2015-088 Recursively searching for this ID in a huge XML file, the script should find this ID and print the elements it has.在一个巨大的 XML 文件中递归搜索这个 ID,脚本应该找到这个 ID 并打印它的元素。

I used content.find("AL2012-2015-088") , but it won't work!我使用了content.find("AL2012-2015-088") ,但它不起作用!

If you would switch to lxml.etree , you would be able to use the full power of XPath expressions (you would also speed things up dramatically).如果您切换到lxml.etree ,您将能够使用 XPath 表达式的全部功能(您还将显着加快速度)。

Here is an example - locating the update element with a desired id and printing out the title :这是一个示例 - 使用所需的id定位update元素并打印出title

from lxml import etree as ET

id_that_user_enters = "AL2012-2015-088"
tree = ET.parse("example.xml")

update = tree.xpath("//update[id = '%s']" % id_that_user_enters)[0]
print(update.findtext("title"))

Prints:印刷:

Amazon Linux 2012.03 - AL2012-2015-088: medium priority package update for gnutls

I believe the find command is designed to find tags as opposed to the text within the tags so you should do find on id .我相信 find 命令旨在查找标签而不是标签中的文本,因此您应该在id查找。 I'm not sure which info you need from the XML, but here is an example that gets the title.我不确定您需要从 XML 获取哪些信息,但这里有一个获取标题的示例。

import xml.etree.ElementTree as elt
content = elt.parse('example.xml').getroot()

def get_id_info(inputID):
    for child in content:
        if child.find('id').text == inputID:
            print child.find('title').text

get_id_info('AL2012-2014-001')

gives Amazon Linux 2012.03 - AL2012-2014-001...Amazon Linux 2012.03 - AL2012-2014-001...

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

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