簡體   English   中英

使用python在xml中顯示元素的內容

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

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元素的內容,比如說一個字符串變量? 我只需要訪問description元素中的數據。

喜歡name = child_node.tag(“description”)

謝謝!

你可以這樣做:

  • 使用ElementTree解析並獲取根
  • 使用root.iter('children')迭代“register”子
  • 根據文檔的結構編寫一些邏輯進行搜索

碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM