ElementTree中如何读取特定子节点的文本?
How do I read the text of specific child nodes in ElementTree?
我正在使用 ElementTree 处理 XML 个文件,每个文件大约有 5000 个这样的 "asset" 节点
<asset id="83">
<name/>
<tag>0</tag>
<vin>3AKJGLBG6GSGZ6917</vin>
<fleet>131283</fleet>
<type id="0">Standard</type>
<subtype/>
<exsid/>
<mileage>0</mileage>
<location>B106</location>
<mileoffset>0</mileoffset>
<enginehouroffset>0</enginehouroffset>
<radioaddress/>
<mfg/>
<inservice>04 Apr 2017</inservice>
<inspdate/>
<status>1</status>
<opstatus timestamp="1491335031">unknown</opstatus>
<gps>567T646576</gps>
<homeloi/>
</asset>
我需要
资产节点上id属性的值
vin节点的文本
gps 节点的文本
如何直接读取 'vin' 和 'gps' 子节点的文本,而不必遍历所有子节点?
for asset_xml in root.findall("./assetlist/asset"):
print(asset_xml.attrib['id'])
for asset_xml_children in asset_xml:
if (asset_xml_children.tag == 'vin'):
print(str(asset_xml_children.text))
if (asset_xml_children.tag == 'gps'):
print(str(asset_xml_children.text))
你可以针对每个asset
元素执行XPath,直接得到vin
和gps
,无需循环:
for asset_xml in root.findall("./assetlist/asset"):
print(asset_xml.attrib['id'])
vin = asset_xml.find("vin")
print(str(vin.text))
gps = asset_xml.find("gps")
print(str(gps.text))
我正在使用 ElementTree 处理 XML 个文件,每个文件大约有 5000 个这样的 "asset" 节点
<asset id="83">
<name/>
<tag>0</tag>
<vin>3AKJGLBG6GSGZ6917</vin>
<fleet>131283</fleet>
<type id="0">Standard</type>
<subtype/>
<exsid/>
<mileage>0</mileage>
<location>B106</location>
<mileoffset>0</mileoffset>
<enginehouroffset>0</enginehouroffset>
<radioaddress/>
<mfg/>
<inservice>04 Apr 2017</inservice>
<inspdate/>
<status>1</status>
<opstatus timestamp="1491335031">unknown</opstatus>
<gps>567T646576</gps>
<homeloi/>
</asset>
我需要
资产节点上id属性的值
vin节点的文本
gps 节点的文本
如何直接读取 'vin' 和 'gps' 子节点的文本,而不必遍历所有子节点?
for asset_xml in root.findall("./assetlist/asset"):
print(asset_xml.attrib['id'])
for asset_xml_children in asset_xml:
if (asset_xml_children.tag == 'vin'):
print(str(asset_xml_children.text))
if (asset_xml_children.tag == 'gps'):
print(str(asset_xml_children.text))
你可以针对每个asset
元素执行XPath,直接得到vin
和gps
,无需循环:
for asset_xml in root.findall("./assetlist/asset"):
print(asset_xml.attrib['id'])
vin = asset_xml.find("vin")
print(str(vin.text))
gps = asset_xml.find("gps")
print(str(gps.text))