简体   繁体   English

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

[英]Display the contents of an element in xml using python

etree to display my xml file, and works great for displaying the attributes of a particular element, but now I need to display the contents of an element. etree显示我的xml文件,非常适合显示特定元素的属性,但现在我需要显示元素的内容。

Input 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 code 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

My question is how can I store the contents of the description element, in a say a string variable? 我的问题是如何存储description元素的内容,比如说一个字符串变量? I just need to access the data in the description element. 我只需要访问description元素中的数据。

like name=child_node.tag("description") 喜欢name = child_node.tag(“description”)

Thanks! 谢谢!

You can do it by: 你可以这样做:

  • Using ElementTree to parse and get the root 使用ElementTree解析并获取根
  • Iterating over "register" children with root.iter('children') 使用root.iter('children')迭代“register”子
  • Writing some logic to search, based on your document's structure 根据文档的结构编写一些逻辑进行搜索

Code: 码:

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 ""

Another code with etree from lxml : 来自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 ""

Output is the same for both examples: 两个示例的输出相同:

[*] 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM