I am trying to parse an XML file with python using lxml, but get an error on basic attempts. I use this post and the lxml tutorials to bootstrap.
My XML file is basically built from records below (I trimmed it down so that it is easier to read):
<?xml version="1.0" ?>
<?xml-stylesheet href="file:///usr/share/nmap/nmap.xsl" type="text/xsl"?>
<nmaprun scanner="nmap" args="nmap -sV -p135,12345 -oX 10.232.0.0.16.xml 10.232.0.0/16" start="1340201347" startstr="Wed Jun 20 16:09:07 2012" version="5.21" xmloutputversion="1.03">
<host>
<hostnames>
<hostname name="host1.example.com" type="PTR"/>
</hostnames>
</host>
</nmaprun>
I run it through this complicated script:
from lxml import etree
d = etree.parse("myfile.xml")
for host in d.findall("host"):
aa = host.find("hostnames/hostname")
print aa.attrib["name"]
I get AttributeError: 'NoneType' object has no attribute 'attrib' on the print line.
I checked the value of d, host and aa and they are all defined as Elements.
Upfront apologies if this is something obvious (and it probably is).
EDIT: I added the header of the XML file as requested (I am still reading and rereading the answers :))
Thanks!
aabe aNoneTypemeans thatfindwasn't able to, well, find anything. As such, this isn't so much an error in the XML-specific code as it is (1) a slightly miswritten search, and (2) a lack of error-checking in handling the output of the lxml library.