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>.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论