使用python / elementree解析xml

时间:2016-06-21 11:30:47

标签: python xml elementtree

我需要搜索的xml指定但不使用命名空间:

    <WRMHEADER xmlns="http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader" version="4.0.0.0">
    <DATA>
        <PROTECTINFO>
            <KEYLEN>16</KEYLEN>
            <ALGID>AESCTR</ALGID>
        </PROTECTINFO>

        <LA_URL>http://192.168.8.33/license/rightsmanager.asmx</LA_URL>
        <LUI_URL>http://192.168.8.33/license/rightsmanager.asmx</LUI_URL>

        <DS_ID></DS_ID>
        <KID></KID>
        <CHECKSUM></CHECKSUM>

    </DATA>
</WRMHEADER>

我想阅读各个字段的值,例如data / protectinfo / keylen等。

root    = ET.fromstring(sMyXml)
keylen  = root.findall('./DATA/PROTECTINFO/KEYLEN')

print root
print keylen

此代码打印以下内容:

<Element {http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader}WRMHEADER at 0x7f2a7c35be60>
[]

root.find和root.findall为此查询返回None或[]。我一直无法指定默认命名空间,是否有查询这些值的解决方案? 感谢

2 个答案:

答案 0 :(得分:1)

创建命名空间dict:

x = """<WRMHEADER xmlns="http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader" version="4.0.0.0">
    <DATA>
        <PROTECTINFO>
            <KEYLEN>16</KEYLEN>
            <ALGID>AESCTR</ALGID>
        </PROTECTINFO>

        <LA_URL>http://192.168.8.33/license/rightsmanager.asmx</LA_URL>
        <LUI_URL>http://192.168.8.33/license/rightsmanager.asmx</LUI_URL>

        <DS_ID></DS_ID>
        <KID></KID>
        <CHECKSUM></CHECKSUM>

    </DATA>
</WRMHEADER>"""
from xml.etree import ElementTree as ET

root = ET.fromstring(x)
ns = {"wrm":"http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader"}
keylen = root.findall('wrm:DATA', ns)

print root
print keylen

现在你应该得到类似的东西:

<Element '{http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader}WRMHEADER' at 0x7fd0a30d45d0>
[<Element '{http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader}DATA' at 0x7fd0a30d4610>]

获取/DATA/PROTECTINFO/KEYLEN

In [17]: root = ET.fromstring(x)

In [18]: ns = {"wrm":"http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader"} 
In [19]: root.find('wrm:DATA/wrm:PROTECTINFO/wrm:KEYLEN', ns).text
Out[19]: '16'

答案 1 :(得分:1)

我想知道这是否也会奏效。请发表您对这种方法的利弊的评论。

from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("xmlquestion.xml")
tn = DOMTree.documentElement
print tn.namespaceURI
#print tn.childNodes

data = tn.getElementsByTagName('DATA')[0]
protectinfo = data.getElementsByTagName('PROTECTINFO')[0]
keylen = protectinfo.getElementsByTagName('KEYLEN')[0]
print keylen.childNodes[0].data

http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader
16