Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:35:50+00:00 2026-06-06T04:35:50+00:00

I am trying to parse an XML file with python using lxml, but get

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:35:52+00:00Added an answer on June 6, 2026 at 4:35 am

    Though it would make more sense to use XPath, your code already works fine when standing alone, so long as one handles the case where a host has no hostnames found:

    doc = lxml.etree.XML("""
      <nmaprun>
        <host>
          <hostnames>
            <hostname name="host1.example.com" type="PTR"/>
          </hostnames>
        </host>
      </nmaprun>""")
    for host in doc.findall('host'):
      host_el = host.find('hostnames/hostname')
      if host_el is not None:
        print host_el.attrib['name']
    

    With XPath (doc.xpath() rather than doc.find() or doc.findall()), one could do better, filtering only for hostnames with a name and thus avoiding the faulty records altogether:

    • host[hostnames/hostname/@name] will find hosts which have at least one hostnames with a hostname with a a name attribute.
    • //hostnames/hostname/@name will directly return only the names themselves (if using lxml, exposing these as strings).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse an xml file that contains accents, but I get this
I'm trying to parse an xml file with PHP. I'm using this code and
I'm trying to parse an xml file using lxml. xml.etree allowed me to simply
I'm trying to parse a XML file using TBXML . However, this parser has
I am trying to parse an XML file using NSXMLParser, but the method [parser
I'm getting this error (see title) while trying to parse an XML file in
I am beginner in PHP. I am trying to parse this xml file. <relationship>
I am trying to parse an XML file. This is the file: <root> <entry>
I'm trying to parse an xml file using XPath DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);
I'm trying to parse a XML file, but when loading it simpleXML prints the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.