开发者

Node.TEXT_NODE has the value, but I need the Attribute

I have an xml file like so:

<host name='ip-10-196-55-2.ec2.internal'>
   <hostvalue name='arch_string'>lx24-x86</hostvalue>
   <hostvalue name='num_proc'>1</hostvalue>
   <hostvalue name='load_avg'>0.01</hostvalue>
 </host>

I can get get out the Node.data from a Node开发者_如何学运维.TEXT_NODE, but I also need the Attribute name, like I want to know load_avg = 0.01, without writing load_avg, num_proc, etc, one by one. I want them all.

My code looks like this, but I can't figure out what part of the Node has the attribute name for me.

    for stat in h.getElementsByTagName("hostvalue"):
        for node3 in stat.childNodes:
            attr = "foo"
            val = "poo"
            if node3.nodeType == Node.ATTRINUTE_NODE:
                attr = node3.tagName
            if node3.nodeType == Node.TEXT_NODE:
                #attr = node3.tagName
                val = node3.data

From the above code, I'm able to get val, but not attr (compile error:


here's a short example of what you could achieve:

from xml.dom import minidom

xmldoc = minidom.parse("so.xml")

values = {}

for stat in xmldoc.getElementsByTagName("hostvalue"):
    attr = stat.attributes["name"].value
    value = "\n".join([x.data for x in stat.childNodes])
    values[attr] = value

print repr(values)

This outputs, given your XML file:

$ ./parse.py 
{u'num_proc': u'1', u'arch_string': u'lx24-x86', u'load_avg': u'0.01'}

Be warned that this is not failsafe, i.e. if you have nested elements inside <hostvalue>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜