繁体   English   中英

如何使用python解析器创建适当的xml属性

[英]How to create proper xml attribute with python parser

from xml.dom.minidom import parse

dom = parse('abc.xml')

for node in dom.getElementsBy('addr'): 
    print node.toxml()  

我需要添加(属性)以仅打印addr(nmap xml文件中的ip地址)?

<host starttime="1404053959" endtime="1404054014"><status state="up" reason="reset" reason_ttl="254"/> <address addr="ip" addrtype="ipv4"/>

我想补充root标签和127.0.0.1192.168.0.1展现更多的东西:

from xml.dom.minidom import parse, parseString

data = '''<root>
<host starttime="1404053959" endtime="1404054014">
    <status state="up" reason="reset" reason_ttl="254"/>
    <address addr="ip" addrtype="ipv4">127.0.0.1</address>
</host>
<host starttime="1404053959" endtime="1404054014">
    <status state="up" reason="reset" reason_ttl="254"/>
    <address addr="ip" addrtype="ipv4">192.168.0.1</address>
</host>
</root>'''

#dom = parse('abc.xml')
dom = parseString(data)

for node in dom.getElementsByTagName('address'):
    print ' xml:', node.toxml()
    print 'type:', node.getAttribute('addrtype')
    print 'addr:', node.childNodes[0].data

结果:

 xml: <address addr="ip" addrtype="ipv4">127.0.0.1</address>
type: ipv4
addr: 127.0.0.1
 xml: <address addr="ip" addrtype="ipv4">192.168.0.1</address>
type: ipv4
addr: 192.168.0.1

暂无
暂无

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

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