'이름이'인 '루트'태그가 여러 개 있습니다. 'name'요소에 의해 사전 순으로 정렬 된 '루트'블록을 정렬하고 싶습니다. lxml/etree/minidom을 사용해 보았지만 작동시키지 못했습니다 ... 태그 내부의 값을 구문 분석하고 부모 루트 태그를 정렬 할 수 없습니다. 여기 자식 요소별로 XML 태그 정렬하기
<?xml version='1.0' encoding='UTF-8'?>
<roots>
<root>
<path>//1.1.1.100/Alex</path>
<name>Alex Space</name>
</root>
<root>
<path>//1.1.1.101/Steve</path>
<name>Steve Space</name>
</root>
<root>
<path>//1.1.1.150/Bethany</path>
<name>Bethanys</name>
</root>
</roots>
내가 시도 것입니다 :
import xml.etree.ElementTree as ET
def sortchildrenby(parent, child):
parent[:] = sorted(parent, key=lambda child: child)
tree = ET.parse('data.xml')
root = tree.getroot()
sortchildrenby(root, 'name')
for child in root:
sortchildrenby(child, 'name')
tree.write('output.xml')
그래서 이름이 가장 먼저 나오길 원하십니까? –