我有一个简单的XML文件,如下所示:
<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>
我想用lxml解析它并得到它的值: 对于brandName,它只需要:
^{pr2}$如果我想了解它的缩写属性。在
'brand_name' : m.findtext(NS+'brandName').attrib['abbrev']
使用maxspeed,我可以通过以下方法获得maxspeed的值:
'maxspeed_value' : m.findtext(NS+'maxspeed/value'),
或者:
'maxspeed_value' : m.find(NS+'maxspeed/value').text,
现在,我想得到单位的属性里面,我尝试了很多不同的方法,但我失败了。大多数情况下的错误是:
'NoneType' object has no attribute 'attrib'
以下是我尝试过但失败的几种方法:
'maxspeed_unit' : m.find(NS+'maxspeed/value').attrib['abbrev'],
'maxspeed_unit' : (m.find(NS+'maxspeed/value'))get('abbrev'),
你能告诉我为什么它不起作用吗? 非常感谢你!在
更新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>
收益率
^{pr2}$lxml元素上的.find方法将只搜索该元素的直接子子级。例如,在这个xml中:
您可以使用根元素.find方法来定位brandname元素或maxspeed元素,但是搜索不会在这些内部元素中遍历。在
例如,你可以这样做:
^{pr2}$从这个返回的元素可以访问属性。在
如果要搜索XML文档中的所有元素,可以使用.iter()方法。对于前面的例子,您可以说:
编辑:下面是一个使用您提供的xml的全功能小示例:
希望有帮助。在
相关问题 更多 >
编程相关推荐