ElementTree - 按名称而不是索引引用 xml 节点
ElementTree - refer to xml node by name rather than index
对于下面的 xml 示例(不是实际的 xml 只是指示性的),对于每个 <field>
记录,我试图:
打印出 title
标签的 ref
属性和 field
标签的 genre
属性,但仅打印出 title
的 ref
属性标记等于 12。对于此 xml,它将打印出:
12, thriller
<?xml version="1.0" encoding="utf-8" ?>
<record>
<field genre='comedy'>
<title ref='123'>Title1</title>
<author>Author1</author>
<example>xml - valid xml file</example>
</field>
<field genre='comedy'>
<title ref='123'>Title1</title>
<author>Author2</author>
<example>xml - valid xml file</example>
</field>
<field genre='thriller'>
<title ref='12'>Title</title>
<author>Author3</author>
<example>xml - valid xml file</example>
</field>
</record>
使用 Element Tree 20.5 documentation 我已经能够使用索引来完成此操作,例如通过引用 child[0]
而不是 field
标签:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for child in root:
if 'ref' in child[0].attrib:
x = child[0].get('ref')
if x == '12':
y = child.get('genre')
print(x, y)
虽然这确实有效,但如果由于某种原因在 child[0]
位置的 xml 中有一个额外的元素,它将无法按要求工作。
如何通过名称而不是索引来引用它?
您可以使用 find('title')
而不是 child[0]
在父元素 <field>
中查找名为 title
的子元素:
for child in root:
title = child.find('title')
if 'ref' in title.attrib:
x = title.get('ref')
if x == '12':
y = child.get('genre')
print(x, y)
对于下面的 xml 示例(不是实际的 xml 只是指示性的),对于每个 <field>
记录,我试图:
打印出 title
标签的 ref
属性和 field
标签的 genre
属性,但仅打印出 title
的 ref
属性标记等于 12。对于此 xml,它将打印出:
12, thriller
<?xml version="1.0" encoding="utf-8" ?>
<record>
<field genre='comedy'>
<title ref='123'>Title1</title>
<author>Author1</author>
<example>xml - valid xml file</example>
</field>
<field genre='comedy'>
<title ref='123'>Title1</title>
<author>Author2</author>
<example>xml - valid xml file</example>
</field>
<field genre='thriller'>
<title ref='12'>Title</title>
<author>Author3</author>
<example>xml - valid xml file</example>
</field>
</record>
使用 Element Tree 20.5 documentation 我已经能够使用索引来完成此操作,例如通过引用 child[0]
而不是 field
标签:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for child in root:
if 'ref' in child[0].attrib:
x = child[0].get('ref')
if x == '12':
y = child.get('genre')
print(x, y)
虽然这确实有效,但如果由于某种原因在 child[0]
位置的 xml 中有一个额外的元素,它将无法按要求工作。
如何通过名称而不是索引来引用它?
您可以使用 find('title')
而不是 child[0]
在父元素 <field>
中查找名为 title
的子元素:
for child in root:
title = child.find('title')
if 'ref' in title.attrib:
x = title.get('ref')
if x == '12':
y = child.get('genre')
print(x, y)