使用xml.dom进行Xml解析

时间:2014-09-05 06:46:54

标签: python xml

我的xmls不像其他人。 这是我的xml的一个例子:

        "<msg t='sys'><body action='verChk' r='0'><ver v='153' /></body></msg>"

我想要的是获得行动的价值。 我如何在python中使用xml.dom ...

1 个答案:

答案 0 :(得分:0)

以下是使用xml.dom并提取action属性值的代码:

s = "<msg t='sys'><body action='verChk' r='0'><ver v='153' /></body></msg>"

from xml.dom import minidom

el = minidom.parseString(s)

el.getElementsByTagName('body')[0].attributes['action'].value
Out[4]: u'verChk'