使用python从具有xmlns命名空间的xml中提取数据

使用python从具有xmlns命名空间的xml中提取数据,python,xml,namespaces,Python,Xml,Namespaces,我有一个名称空间为的XML: <metadata xmlns="http://example.com"> <samples> <sample> <hashes> <hash type="md5">Abc6FC6F4AA4C5315D2A52E29865F7F6</hash> </hashes> <detections> <det

我有一个名称空间为的XML:

<metadata xmlns="http://example.com">


<samples>
<sample>

    <hashes>
        <hash type="md5">Abc6FC6F4AA4C5315D2A52E29865F7F6</hash>
    </hashes>


    <detections>

        <detection vendor="example_1" date="2015-02-17T01:55:38" type="human" >

            <![CDATA[my_detection1]]>

        </detection>

        <detection vendor="example_2" date="2015-02-17T01:55:38" type="computer" >

            <![CDATA[my_detection2]]>


        </detection>

    </detections>
</sample>

<sample>

    <hashes>
        <hash type="md5">CDEFC6F4AA4C5315D2A52E29865F7F6</hash>
    </hashes>


    <detections>

        <detection vendor="example_3" date="2015-02-17T01:55:38" type="human" >

              <![CDATA[my_detection3]]>

        </detection>

        <detection vendor="example_4" date="2015-02-17T01:55:38" type="computer" >

              <![CDATA[my_detection4]]>

        </detection>

    </detections>
</sample>
</samples>
</metadata>

Abc6FC6F4AA4C5315D2A52E29865F7F6
CDEFC6F4AA4C5315D2A52E29865F7F6
我希望提取数据,以便:

如果特定的“md5”匹配,则检查“检测”中的“供应商”属性,如果匹配,则提取属性“日期”和文本值(例如:“我的检测1”)


该文件将非常大,包含许多“示例”标记。谢谢。

谢谢大家!最后我发现了如何实现这一点。 python中的DOM最适合执行需要大量if/else类型操作的困难XML操作:

import xml.dom.minidom
from xml.dom.minidom import Node

dom = xml.dom.minidom.parse("C:/tmp/merged.xml")

hash_node=dom.getElementsByTagName('hash')

md5='7CD6FC6F4AA4C5315D2A52E29865F7F6'

for node1 in hash_node:

    str1 = node1.childNodes[0].wholeText

    if (str1 == md5):

        hashes_node = node1.parentNode
        sample_node = hashes_node.parentNode
        detection_node = sample_node.getElementsByTagName('detection')


        print ("For MD5 " + md5 + ",\n\n")
        for node2 in detection_node:

            print (node2.childNodes[0].wholeText)

md5哈希需要匹配什么?你是否已经通过其他方式在内存中存储了一组md5哈希?是的,皮特!我将使用具有md5值的参数进行函数调用。