Parse xml with python
Question: 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>
    


Answer: There can be more than one `address`. So use

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

to get the first `address`.

