使用python在xml中显示元素的内容

时间:2015-11-02 22:54:07

标签: python xml lxml

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

我的问题是如何将一个描述元素的内容存储在一个字符串变量中?我只需要访问description元素中的数据。

像     名称= child_node.tag(&#34;描述&#34)

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式完成:

  • 使用 ElementTree 解析并获取根
  • 使用 root.iter('children')
  • 对“注册”孩子进行迭代
  • 根据您的文档结构编写一些逻辑进行搜索

代码:

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