迭代地在XML中附加子元素

时间:2016-06-16 08:48:27

标签: python lxml

我有一个结构如下的xml:

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
    <route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4  -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>

有多个vType元素(例如公共汽车,出租车,乘用车等),对于每个vType,有vehicle的多个即时对应(编号为1.0,1.1等)将路径边作为属性。

我现在要附加文件,以便我现在在车辆下面有一个子元素stop,指定止损如下

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
    <stop lane="207358226" endPos="10" duration="20"/>
    <stop lane="206878618#0" endPos="10" duration="20"/>
    <stop lane="206878618#1" endPos="10" duration="20"/>
    ..........
    ..........
</vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
    <route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4  -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
    <stop lane="207358226" endPos="10" duration="20"/>
    <stop lane="206878618#0" endPos="10" duration="20"/>
    <stop lane="206878618#1" endPos="10" duration="20"/>
    ..........
    ..........
</vehicle>

我最初的方法是迭代解析xml并选取带有标记vehicle和属性Bus的元素。然后我将边缘复制到列表edgesnew中。然后,我在一个名为stop的车辆下的循环内迭代地创建一个子元素。代码如下

parser = etree.XMLParser(encoding='utf-8', recover=True)
routesFileTree = etree.parse('kaiserslautern.rou1.xml', parser)
routesFileRoot = routesFileTree.getroot()
vehicle = routesFileRoot.find('vehicle')
route = etree.SubElement(vehicle, 'route')

for elem in routesFileRoot.iter(tag = 'vehicle'):
if elem.attrib['type'] == 'Bus':
    for subelem in elem.iter(tag = 'route'):
        if subelem.attrib.get('edges'):
            edgesnew = subelem.attrib['edges'].split(' ')
        for edges in range(0,len(edgesnew),3):
            stop = etree.SubElement(vehicle,'stop', lane = stops[edgesnew[edges]], duration = "30")

程序执行但我的算法错误,因为当我尝试打印

时它返回以下输出
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
<route><stop lane="207358226" duration="30" endPos="10"/><stop lane="206878618#0" duration="30" endPos="10"/><.........../></route></vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
    <route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4  -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
    <route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>

代码中存在多个问题。首先,它仅为车辆的一个实例化创建新的子元素。其次,它创建新的route元素,而不是附加到现有的xml。我已经看到我需要使用element.append,但无法弄清楚在哪里和如何。

提前感谢您的帮助

0 个答案:

没有答案