I know this question has been asked before but I am struggling to get it to work with my example and would really appreciate some help. What I am trying to achieve seems fai
Using ElementTree you can do this:
import xml.etree.ElementTree as ET
def sortchildrenby(parent, attr):
parent[:] = sorted(parent, key=lambda child: child.get(attr))
tree = ET.parse('input.xml')
root = tree.getroot()
sortchildrenby(root, 'NAME')
for child in root:
sortchildrenby(child, 'NAME')
tree.write('output.xml')