在xml中搜索文本并返回元素/节点

时间:2013-08-18 00:55:23

标签: python xml search text

我希望能够通过文本值搜索xml格式的文件并返回它所属的id。我在xml命令中查看了python库,但只看到了按元素/节点搜索的示例。我在下面有一个简化的xml示例,我想搜索“3x3 Eyes”,例如返回“2”。它还应该搜索确切的文本减去案例。每个动画下通常会有多个标题条目,因此搜索可以在第一场比赛时停止。感谢

<?xml version="1.0" encoding="UTF-8"?>
<animetitles>
  <anime aid="1">
    <title type="official" xml:lang="fr">Crest of the Stars</title>
    <title type="official" xml:lang="fr">Crest of the Stars</title>
  </anime>
  <anime aid="2">
    <title type="official" xml:lang="en">3x3 Eyes</title>
  </anime>
  <anime aid="3">
    <title type="official" xml:lang="en">3x3 Eyes: Legend of the Divine Demon</title>
  </anime>
</animetitles>

2 个答案:

答案 0 :(得分:1)

tree = et.parse( ... )

# Unique match
results = []
for anime in tree.findall('anime'):
    for title in anime.findall('title'):
        if title.text == '3x3 Eyes':
            results.append(anime.get('aid'))
print results

# Everything that starts with
results = []
for anime in tree.findall('anime'):
    for title in anime.findall('title'):
        if title.text.startswith('3x3 Eyes'):
            results.append(anime.get('aid'))
print results

第一个返回[2],第二个返回[2, 3]

或者有点神秘,但是,嘿,为什么不:)

results = [anime.get('aid') for anime in tree.findall('anime')
           for title in anime.findall('title') if title.text == '3x3 Eyes']

答案 1 :(得分:0)

您可以将ElementTree用于您的目的。

import xml.etree.ElementTree as ET
tree = ET.parse('a.xml')
root = tree.getroot()

def findParentAttrib(string):
    for neighbor in root.iter():
        for parent in neighbor.getiterator():
            for child in parent:
                if child.text == string:
                    return parent.attrib['aid']

print findParentAttrib("3x3 Eyes") # returns 2

另请参阅this page