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

  • SEARCH
  • Home
  • 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 6823975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:50:50+00:00 2026-05-26T21:50:50+00:00

I have a simple file XML like below: <brandName type=http://example.com/codes/bmw# abbrev=BMW value=BMW />BMW</brandName> <maxspeed>

  • 0

I have a simple file XML like below:

    <brandName type="http://example.com/codes/bmw#" abbrev="BMW" value="BMW" />BMW</brandName>
      <maxspeed>
        <value>250</value>
        <unit type="http://example.com/codes/units#" value="miles per hour" abbrev="mph" />
      </maxspeed>

I want to parse it using lxml and get the value of it:
With brandName, it just need:

    'brand_name'  : m.findtext(NS+'brandName')

If I want to get into abbrev attribute of it.

    'brand_name'  : m.findtext(NS+'brandName').attrib['abbrev']

With maxspeed, i can get the value of maxspeed by:

    'maxspeed_value'                  : m.findtext(NS+'maxspeed/value'),

or:

    'maxspeed_value'                  : m.find(NS+'maxspeed/value').text,

Now, I want to get the attribute of unit inside , I have tried a lot of different way but I’m failed. The error most of time is:

    'NoneType' object has no attribute 'attrib'

Here are several ways I tried and it failed:

    'maxspeed_unit'                  : m.find(NS+'maxspeed/value').attrib['abbrev'],
    'maxspeed_unit'                  : (m.find(NS+'maxspeed/value'))get('abbrev'),

Could you please give me some hint why it doesn’t work?
Thank you very much!

UPDATE XML:

    <Car xmlns="http://example.com/vocab/xml/cars#">
     <dateStarted>2011-02-05</dateStarted>
     <dateSold>2011-02-13</dateSold>
    <name type="http://example.com/codes/bmw#" abbrev="X6" value="BMW X6" >BMW X6</name>
    <brandName type="http://example.com/codes/bmw#" abbrev="BMW" value="BMW" />BMW</brandName>
      <maxspeed>
        <value>250</value>
        <unit type="http://example.com/codes/units#" value="miles per hour" abbrev="mph" />
      </maxspeed>
      <route type="http://example.com/codes/routes#" abbrev="HW" value="Highway" >Highway</route>
      <power>
        <value>180</value>
        <unit type="http://example.com/codes/units#" value="powerhorse" abbrev="ph" />
      </power>
      <frequency type="http://example.com/codes/frequency#" value="daily" >Daily</frequency>  
    </Car>
  • 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-05-26T21:50:51+00:00Added an answer on May 26, 2026 at 9:50 pm

    The .find method on an lxml Element will only search the direct sub-children of that element. so for example in this xml:

    <root>
        <brandName type="http://example.com/codes/bmw#" abbrev="BMW" value="BMW">BMW</brandName>
        <maxspeed>
            <value>250</value>
            <unit type="http://example.com/codes/units#" value="miles per hour" abbrev="mph" />
        </maxspeed>
    </root>
    

    You can use the root Elements .find method to locate the brandname element, or the maxspeed element, but the search will not traverse inside these inner elements.

    So you could for example do something like this:

    root.find('maxspeed').find('unit') #returns the unit Element
    

    From this returned element you can access the attributes.

    If you’d like to search through all the elements within an XML doc, you can use the .iter() method. So for the previous example you could say:

    for element in root.iter(tag='unit'):
        print element #This would print all the unit elements in the document.
    

    EDIT: Here is a small fully functional example using the xml you provided:

    import lxml.etree
    from StringIO import StringIO
    
    def ns_join(element, tag, namespace=None):
        '''Joins the namespace and tag together, and
        returns the fully qualified name.
        @param element - The lxml.etree._Element you're searching
        @param tag - The tag you're joining
        @param namespace - (optional) The Namespace shortname default is None'''
    
        return '{%s}%s' % (element.nsmap[namespace], tag)
    
    def parse_car(element):
        '''Parse a car element, This will return a dictionary containing
        brand_name, maxspeed_value, and maxspeed_unit'''
    
        maxspeed = element.find(ns_join(element,'maxspeed'))
        return { 
            'brand_name' : element.findtext(ns_join(element,'brandName')), 
            'maxspeed_value' : maxspeed.findtext(ns_join(maxspeed,'value')), 
            'maxspeed_unit' : maxspeed.find(ns_join(maxspeed, 'unit')).attrib['abbrev']
            }
    
    #Create the StringIO object to feed to the parser.
    XML = StringIO('''
    <Reports>
        <Car xmlns="http://example.com/vocab/xml/cars#">
            <dateStarted>2011-02-05</dateStarted>
            <dateSold>2011-02-13</dateSold>
            <name type="http://example.com/codes/bmw#" abbrev="X6" value="BMW X6" >BMW X6</name>
            <brandName type="http://example.com/codes/bmw#" abbrev="BMW" value="BMW" >BMW</brandName>
            <maxspeed>
                <value>250</value>
                <unit type="http://example.com/codes/units#" value="miles per hour" abbrev="mph" />
            </maxspeed>
            <route type="http://example.com/codes/routes#" abbrev="HW" value="Highway" >Highway</route>
            <power>
                <value>180</value>
                <unit type="http://example.com/codes/units#" value="powerhorse" abbrev="ph" />
            </power>
            <frequency type="http://example.com/codes/frequency#" value="daily" >Daily</frequency>  
        </Car>
    </Reports>
    ''')
    
    #Get the root element object of the xml
    car_root_element = lxml.etree.parse(XML).getroot()
    
    # For each 'Car' tag in the root element,
    # we want to parse it and save the list as cars
    cars = [ parse_car(element) 
        for element in car_root_element.iter() if element.tag.endswith('Car')]
    
    print cars
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple XML file like this: <?xml version=1.0 encoding=UTF-8?> <?xml-stylesheet type=text/xsl href=01C3_OIZODEMO_certificato_v1.0.xsl?>
I have a simple but huge xml file like below. I want to parse
I have an xml file which looks like the below example <Ids> <Id> <set>ai</set>
I have a simple XML file like so: <?xml version=1.0 encoding=UTF-8?> <foo attr=blah &#176;
I have an XML file (example below) and it is sitting somewhere on the
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
i have a simple xml file in a wcf service that i am trying
I have a simple xml file and I want to remove everything before the
I have a really simple XML file that I'm trying to read, but I
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>

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.