我有以下xml文件数据:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<rootnode>
<TExportCarcass>
<BodyNum>6168</BodyNum>
<BodyWeight>331.40</BodyWeight>
<UnitID>1</UnitID>
<Plant>239</Plant>
<pieces>
<TExportCarcassPiece index="0">
<Bruising>0</Bruising>
<RFIDPlant></RFIDPlant>
</TExportCarcassPiece>
<TExportCarcassPiece index="1">
<Bruising>0</Bruising>
<RFIDPlant></RFIDPlant>
</TExportCarcassPiece>
</pieces>
</TExportCarcass>
<TExportCarcass>
<BodyNum>6169</BodyNum>
<BodyWeight>334.40</BodyWeight>
<UnitID>1</UnitID>
<Plant>278</Plant>
<pieces>
<TExportCarcassPiece index="0">
<Bruising>0</Bruising>
<RFIDPlant></RFIDPlant>
</TExportCarcassPiece>
<TExportCarcassPiece index="1">
<Bruising>0</Bruising>
<RFIDPlant></RFIDPlant>
</TExportCarcassPiece>
</pieces>
</TExportCarcass>
</rootnode>
我使用python的lxml
模块从xml文件读取数据,如下所示:
但它只打印None
,而不是6168
。请告诉我我做错了什么。在
您需要迭代每个}
TExportCarcass
标记,然后使用find
访问{例如:
输出:
^{pr2}$或者
对文本字符串运行
find
时,它将只搜索根级别的元素。您可以在find
中使用xpath
查询来搜索文档中的任何元素:- 要获取所有元素:
^{pr2}$1-使用
/
指定要提取的元素的树级别2-使用
.text
提取元素的名称相关问题 更多 >
编程相关推荐