python: error with basic XML parsing (with lxml)

DOUGHBOY

New Member
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):\[code\]<?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>\[/code\]I run it through this complicated script:\[code\]from lxml import etreed = etree.parse("myfile.xml")for host in d.findall("host"): aa = host.find("hostnames/hostname") print aa.attrib["name"]\[/code\]I get \[code\]AttributeError: 'NoneType' object has no attribute 'attrib'\[/code\] on the \[code\]print\[/code\] line.I checked the value of \[code\]d\[/code\], \[code\]host\[/code\] and \[code\]aa\[/code\] 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!
 
Back
Top