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>
The .find method on an lxml Element will only search the direct sub-children of that element. so for example in this xml:
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:
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:
EDIT: Here is a small fully functional example using the xml you provided:
Hope it helps.