1

I'm trying to parse a XML document with Python, this is my code:

from xml.dom import minidom

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

hosts = xmldoc.getElementsByTagName("host")

for host in hosts:
    address = host.getElementByTag("address")
    ip = address.attributes["addr"]
    IP = ip.value
    print("IP:%s"%(IP))

this returns me:

Traceback (most recent call last):
File "simple.py", line 8, in <module>
address = host.getElementByTag("address")
AttributeError: Element instance has no attribute 'getElementByTag'

The XML file:

<?xml version="1.0"?>
<!DOCTYPE nmaprun>
<?xml-stylesheet href="file:///usr/local/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 6.47 scan initiated Thu Feb 26 11:38:24 2015 as: nmap -oX aula.xml x.x.x.x/26 -->
<nmaprun scanner="nmap" args="nmap -oX aula.xml x.x.x.x/26" start="1424947104" startstr="Thu Feb 26 11:38:24 2015" version="6.47" xmloutputversion="1.04">
  <scaninfo type="connect" protocol="tcp" numservices="1000" services="a lot of numbers"/>
  <verbose level="0"/>
  <debugging level="0"/>
  <host starttime="1424947104" endtime="1424947111"><status state="up" reason="conn-refused" reason_ttl="0"/>
    <address addr="x.x.x.x" addrtype="ipv4"/>
    <hostnames>
    </hostnames>
    <ports>
        <extraports state="closed" count="998">
            <extrareasons reason="conn-refused" count="998"/>
        </extraports>
        <port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
        <port protocol="tcp" portid="111"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="rpcbind" method="table" conf="3"/></port>
    </ports>
    <times srtt="1280" rttvar="264" to="100000"/>
</host>
</nmaprun>
0

3 Answers 3

0
  1. No getElementByTag method:: There is no such method. Use getElementsByTagName() method to get the address tag list.
  2. Check if list have items or not then get addr attribute value from the address tag. 3.

Demo:

from xml.dom import minidom

p = '/home/vivek/Desktop/Work/input_minidom.xml'
xmldoc = minidom.parse(p)

hosts = xmldoc.getElementsByTagName("host")

for host in hosts:
    address = host.getElementsByTagName("address")
    if address:
        ip = address[0].attributes["addr"]
        IP = ip.value
        print("IP:%s"%(IP))

Output:

$ python task4.py 
IP:x.x.x.x
2
  • @CristinaNogués: Welcome. If you have any issue related to python you can ask on stackoverflow or I can be reached anytime via email at [email protected], for my skype id: vivek.igp. If answer is helpful to you then you can up vote and accept answers on stackoverflow. Mar 2, 2015 at 11:27
  • Thank you! I tried to vote up your answer but it seems that I need to have 15 points reputation. Mar 3, 2015 at 14:40
0

There can be more than one address. So use

address = host.getElementsByTagName("address")[0]

to get the first address.

0

The method you are looking for is getElementsByTagName, not getElementByTag. The return value is a list of tags matching, so you'll have to iterate through it. For instance:

from xml.dom import minidom

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

hosts = xmldoc.getElementsByTagName("host")

for host in hosts:
    addresses = host.getElementsByTagName("address")
    for address in addresses:
        ip = address.attributes["addr"]
        IP = ip.value
        print("IP:%s"%(IP))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.