Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the contents of an element in xml using python

Tags:

python

xml

lxml

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.

Input 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

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? I just need to access the data in the description element.

like name=child_node.tag("description")

Thanks!

like image 841
GoldenEagle Avatar asked Oct 24 '25 05:10

GoldenEagle


1 Answers

You can do it by:

  • Using ElementTree to parse and get the root
  • Iterating over "register" children with root.iter('children')
  • 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:

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
like image 77
Andrés Pérez-Albela H. Avatar answered Oct 26 '25 19:10

Andrés Pérez-Albela H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!