1

I have XML document

<Mapping name="AccountBalanceInquiryResolving" areaSize="31000" module="CCS">
  <Input attribute="casHeaId" ldb="AccountBalanceInquiryResolvingLDB" to="casHeaId" dataType="hr.vestigo.framework.util.vtype.VBigDecimal"/>
</Mapping>

I need to delete from root tag Mapping everything except name, and module. to be precise i need to delete attribure areaSize.

for filename in os.listdir(path):
    if not filename.endswith('.xml'):
        continue
    fullname = os.path.join(path, filename)
    tree = ET.parse(fullname)
    root = tree.getroot()

    if root.tag == 'Mapping':
            mapping_name= root.get('name')
            mapping_module= root.get('module')
            for atr in root.attrib:
                if atr != 'name' and atr !='module':
                print(atr)

print result is good , it is areaSize. How can i remove it from my xml document?

1 Answers1

1

here is the solution

for filename in os.listdir(path):
    if not filename.endswith('.xml'):
        continue
    fullname = os.path.join(path, filename)
    tree = ET.parse(fullname)
    root = tree.getroot()
    
    if root.tag == 'Mapping':
        undesired = []
        mapping_name= root.get('name')
        mapping_module= root.get('module')
        for atr in root.attrib:
            if atr != 'name' and atr !='module':
                 undesired.append(atr) 
        for i in undesired:
            print i
            try:
                del root.attrib[i]
            except KeyError:
                pass
            tree.write('./xmlDirectory/otput.xml')