GoldenEagle
GoldenEagle

Reputation: 125

How can I display the child element of a node from an xml file, in python?

Here is my xml file

<root>
        <Module name="ac4" offset="32" width="12">
            <register name="xga_control" offset="0x000" width="32" access="R/W">

                <field name="reserved" offset="0" bit_span="5"/>

                <field name="force_all_fault_clear" bit_span="1" default="0">
                    <description>Rising edge forces all fault registers to clear</description>
                </field>

                <field name="force_warning" default="0" bit_span="1">
                    <description>Forces AC2 to report a Master Warning</description>
                </field>

                <field name="force_error" default="0" bit_span="1">
                   <description>Forces AC2 to report a Master Error</description>
                </field>
            </register>
         </Module>

<root>

Right now I can access the names of my registers and display them. However I also want to display the names and attributes of my field elements. How can I do that? Here is my code so far.

input_file = etree.parse('file1.xml')
output=open("ac4.vhd","w+")

    output.write("Registers \n")
    for node in input_file.iter():
        if node.tag=="register":
            name=node.attrib.get("name")
            print(name)
            output.write(name)
            output.write("\n")
            if node.tag=="field":
                name=node.attrib.get("name")
                output.write(name)

Right now the output looks like

Registers 
xga_control
i_cmd_reg

I want it to look like

Registers 
xga_control
    reserved           
    force_all_fault_clear
    force_warning
    force_error
i_cmd_reg
   field name
   field name

Any ideas on how to do this?

Upvotes: 0

Views: 96

Answers (1)

dopstar
dopstar

Reputation: 1488

Instead of iterating over input_file.iter() you can do input_file.getroot() and iterate systematically over that.

This is how you would write your code:

import xml.etree.ElementTree as ET                                               

tree = ET.parse('file1.xml')                                                     

root = tree.getroot()                                                            

with open('ac4.vhd', 'w+') as fd:                                                
    fd.write('Registers\n')                                                      
    for node in root:                                                            
        if node.tag == 'Module':                                                 
            for sub_node in node:                                                
                fd.write('{0}\n'.format(sub_node.get('name')))                   
                for child in sub_node:                                           
                    fd.write('\t{0}\n'.format(child.get('name')))

Your output becomes:

Registers
xga_control
    reserved
    force_all_fault_clear
    force_warning
    force_error

Upvotes: 1

Related Questions