etree 用于显示我的 xml 文件,并且非常适合显示特定元素的属性,但现在我需要显示元素的内容。
输入xml
<root>
<Module>
<register name="i_cmd_reg" offset="0x004" width="20" access="R/W" >
<field name="value" default="0" bit_span="20">
<description>System gradient driver current command - 1.72mA/LSB</description>
</field>
</register>
<register name="i_ecc_cmd_reg" offset="0x008" width="20" access="R/W" >
<description>Calculated ECC current command - 1.72mA/LSB</description>
<field name="field1"/>
</register>
</Module>
</root>
Python代码
from lxml import etree
xml_file = etree.parse('file1.xml')
input_1=open("sample_template.txt","r")
output=open("output.txt","w+")
i=0
k=0
for node in input_file.iter():
if node.tag=="register":
register[i]['name']=node.attrib.get("name")
register[i]['offset']=node.attrib.get("offset")
k=0
for child_node in node:
if child_node.tag=="field":
register[i]['fields'][k]['name']=child_node.attrib.get("name")
register[i]['fields'][k]['offset']=child_node.attrib.get("offset")
k+=1
我的问题是如何将描述元素的内容存储在一个字符串变量中?我只需要访问描述元素中的数据。
喜欢 name=child_node.tag("描述")
谢谢!
最佳答案
你可以这样做:
- 使用ElementTree解析并获取根
- 使用 root.iter('children') 迭代“注册” child
- 根据文档的结构编写一些搜索逻辑
代码:
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
for child in root.iter('register'):
name = child.attrib.get('name', None)
offset = child.attrib.get('offset', None)
width = child.attrib.get('width', None)
access = child.attrib.get('access', None)
description = child.find('description')
if description is not None:
desc = description.text
else:
desc = child.find('field').find('description').text
print "[*] Register Name: {}".format(name)
print "[*] Register Offset: {}".format(offset)
print "[*] Register Width: {}".format(width)
print "[*] Register Access: {}".format(access)
print "[*] Description: {}".format(desc)
print ""
来自 lxml 的 etree 的另一个代码:
from lxml import etree
with open('file.xml') as xml_file:
tree = etree.fromstring(xml_file.read())
for child in tree.xpath('//root/Module/register'):
name = ''.join(child.xpath('./@name'))
offset = ''.join(child.xpath('./@offset'))
width = ''.join(child.xpath('./@width'))
access = ''.join(child.xpath('./@access'))
description = ''.join(child.xpath('.//description/text()'))
print "[*] Register Name: {}".format(name)
print "[*] Register Offset: {}".format(offset)
print "[*] Register Width: {}".format(width)
print "[*] Register Access: {}".format(access)
print "[*] Description: {}".format(description)
print ""
两个示例的输出相同:
[*] Register Name: i_cmd_reg
[*] Register Offset: 0x004
[*] Register Width: 20
[*] Register Access: R/W
[*] Description: System gradient driver current command - 1.72mA/LSB
[*] Register Name: i_ecc_cmd_reg
[*] Register Offset: 0x008
[*] Register Width: 20
[*] Register Access: R/W
[*] Description: Calculated ECC current command - 1.72mA/LSB
关于python - 使用python显示xml中元素的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33488518/