繁体   English   中英

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

[英]using python extract data from xml having xmlns namespace

我有一个具有命名空间的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>

我想提取数据,以便:

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

该文件将非常大,包含大量“样本”标记。 谢谢。

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

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)

暂无
暂无

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

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