Search code examples
pythonxmlelementtree

write a xml attributes to a list


I want to write all the sourcefields.attrib of a xml tag to a list. I have written the code to fetch all the attributes but want to append to a single list.

import xml.etree.ElementTree as ET

if not ET.parse('C:\\Users\\dd00849401\\Desktop\\xml\\m_DM_DIM_NRC_CUSTOMER.xml'):
    raise SyntaxError
else:
   print("The XML is valid")
   tree = ET.parse('C:\\Users\\dd00849401\\Desktop\\xml\\m_DM_DIM_NRC_CUSTOMER.xml')
   root = tree.getroot()

dicts = []
d = {}
print("*********** The Source details ***********")
for source in root.iter('SOURCE'):
    print("\n")
    sourcename = source.attrib['NAME']
    print(sourcename)
    print("*********** The DETAILS of  SOURCE: %s: : ***********" %sourcename)
    print(source.attrib)
    print("*********** The COLUMN NAMES of  SOURCE: %s ***********" % sourcename)
    for sourcefields in source.iter("SOURCEFIELD"):
        print(sourcefields.attrib)

Presntly i am getting:

 {'BUSINESSNAME': '', 'DATATYPE': 'varchar', 'DESCRIPTION': '', 'FIELDNUMBER': '1'}
{'BUSINESSNAME': '', 'DATATYPE': 'numeric', 'DESCRIPTION': '', 'FIELDNUMBER': '2'}
{'BUSINESSNAME': '', 'DATATYPE': 'timestamp', 'DESCRIPTION': '', 'FIELDNUMBER': '3'}

I want it as :

[{'BUSINESSNAME': '', 'DATATYPE': 'varchar', 'DESCRIPTION': '', 'FIELDNUMBER': '1'},
{'BUSINESSNAME': '', 'DATATYPE': 'numeric', 'DESCRIPTION': '', 'FIELDNUMBER': '2'},
{'BUSINESSNAME': '', 'DATATYPE': 'timestamp', 'DESCRIPTION': '', 'FIELDNUMBER': '3'}]

Solution

  • You can simply initialize an empty list outside of your loop and then append each attribute dictionary to this list within the loop

    example:

    import xml.etree.ElementTree as ET
    
    xml_file = 'C:\\Users\\dd00849401\\Desktop\\xml\\m_DM_DIM_NRC_CUSTOMER.xml'
    tree = ET.parse(xml_file)
    root = tree.getroot()
    
    sourcefields_attributes = []
    
    print("*********** The Source details ***********")
    for source in root.iter('SOURCE'):
        sourcename = source.attrib['NAME']
        print("\n")
        print(sourcename)
        print("*********** The DETAILS of  SOURCE: %s: : ***********" % sourcename)
        print(source.attrib)
        print("*********** The COLUMN NAMES of  SOURCE: %s ***********" % sourcename)
    
        for sourcefields in source.iter("SOURCEFIELD"):
            print(sourcefields.attrib)
            sourcefields_attributes.append(sourcefields.attrib)
    
    print(sourcefields_attributes)