如何按子元素对XML排序?

问题描述

我正在使用Python使用ElementTree解析xml数据。我有需要按子元素值排序的xml数据。下面我有xml数据。我需要按子元素“时间”对完整的节点“ TheSong”进行排序,希望它们从最短时间到最高时间排序。我不确定如何执行此操作,但是我已经完成了以下操作来解析xml数据

func viewDidLoad() {
    super.viewDidLoad()
    let tapRecognizer = UITapGestureRecognizer(target: self,action: #selector(mapViewDidTap(_:)))
    mapView.addGestureRecognizer(tapRecognizer)
    ...
}

func mapViewDidTap(_ tapRecognizer: UITapGestureRecognizer) {
    if tapRecognizer.state == .recognized {
        // Get map coordinate from touch point
        let touchCoord = tapRecognizer.location(in: mapView)
        let coord = mapView.convert(touchCoord,toCoordinateFrom: mapView)
        let coordRect = MKMapRect(origin: coord,size: MKMapSize())
        // for every overlay ...
        for overlay in mapView.overlays {
            // .. if Mypolygon ...
            if let polygon = overlay as? Mypolygon {
                let selected = polygon.intersects(coordRect)
                if polygon.selected != selected {
                    polygon.selected = selected
                    mapView.renderer(for: polygon)?.setNeedsdisplay()
                }
            }
        }      
    }
}
import xml.etree.ElementTree as ET

tree = ET.parse('songs.xml')
root = tree.getroot()

for child in root:
    //My task
    print("Everything in correct order")

输出应以此顺序


<Songs>
    <Songs>
        <TheSong Id="0">
            <LomId Value="0"/>
            <Time Value="36"/>
            <Name Value="Intro"/>
            <Annotation Value=""/>
            <IsSongStart Value="false"/>
        </TheSong>
        <TheSong Id="1">
            <LomId Value="0"/>
            <Time Value="4"/>
            <Name Value="Vamp"/>
            <Annotation Value=""/>
            <IsSongStart Value="false"/>
        </TheSong>
        <TheSong Id="2">
            <LomId Value="0"/>
            <Time Value="12"/>
            <Name Value="Verse"/>
            <Annotation Value=""/>
            <IsSongStart Value="false"/>
        </TheSong>
        <TheSong Id="3">
            <LomId Value="0"/>
            <Time Value="0"/>
            <Name Value="Chous"/>
            <Annotation Value=""/>
            <IsSongStart Value="false"/>
        </TheSong>
        <TheSong Id="4">
            <LomId Value="0"/>
            <Time Value="30"/>
            <Name Value="ending"/>
            <Annotation Value=""/>
            <IsSongStart Value="false"/>
        </TheSong>
    </Songs>
</Songs>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)