I am using the below code to parse an xml and get the different attributes of an element. I need the name,datatype,length,is_autonumber and ismandatory properties of each attribute. Some attributes don’t have some of these properties. Iam parsing and storing the values in lists and then creating a dataframe and converting it into an xml. Since my original xml is very complex I am using this code to get a simplified xml with only the entities and attributes I need.
When I run this code I get “All Arrays must be of the same length”. Please find the screenshot.
import pandas as pd
import xml.etree.ElementTree as et
tree = et.parse('xmlfile.xml')
root = tree.getroot()
entity_list = root.findall('.//Entity')
entity_names = [e.get('Name') for e in entity_list]
attribute_lists = []
attribute_dtype_lists = []
attribute_ismandate = []
attribute_length = []
attribute_autonum = []
for entity in entity_list:
attributes = ["Name = " + n.get('Name') for n in entity.findall('.//Attribute')]
attribute_dtype = ["Datatype = " + d.get('DataType') for d in entity.findall('.//Attribute')]
attribute_ismandate = ["IsMandatory =" + str(m.get('IsMandatory')) for m in entity.findall('.//Attribute')]
attribute_length = ["Length =" + str(l.get('Length')) for l in entity.findall('.//Attribute')]
attribute_autonumber = ["IsAutoNumber =" + str(a.get('IsAutoNumber')) for a in entity.findall('.//Attribute')]
attribute_lists.append(attributes)
attribute_dtype_lists.append(attribute_dtype)
attribute_ismandate.append(attribute_ismandate)
attribute_length.append(attribute_length)
attribute_autonum.append(attribute_autonumber)
df= pd.DataFrame({'Table':entity_names, 'Attributes':attribute_lists, 'Datatype':attribute_dtype_lists, 'IsMandatory':attribute_ismandate,'Islength':attribute_length,'IsAutonumber':attribute_autonum})
df.columns = ['Table', 'Attributes','Datatype','IsMandatory','Islength','IsAutonumber']
df.to_xml('data.xml')
I am supposed to get an xml with all the attributes and its properties. I convert the xml to dataframe and use df.to_xml. This code was working fine when I had only the Name and Datatype properties included. But when I included the lists to store length and other properties, I get the above error.
The original xml looks like this :
I will be thankful if anybody could help me solve this as I am a beginner in python. Thank you