簡體   English   中英

用python腳本修改xml文件

[英]Modify xml file with python script

如何修改以下 xml 片段

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
    <vType id="car1_73" length="4.70" minGap="1.00" maxSpeed="12.76" probability="0.00" vClass="passenger" guiShape="passenger/van">
        <carFollowing-Krauss accel="2.40" decel="4.00" sigma="0.55"/>
    </vType>
    <vehicle id="0" type="vTypeDist" depart="0.00" departLane="best" departPos="random" departSpeed="random">
        <routeDistribution last="1">
            <route cost="108.41" probability="0.44076116" edges="bottom7to7/0 7/0to6/0 6/0to6/1 6/1to5/1 5/1to5/2 5/2to6/2"/>
            <route cost="76.56" probability="0.55923884" edges="bottom7to7/0 7/0to6/0 6/0to5/0 5/0to5/1 5/1to5/2 5/2to6/2"/>
        </routeDistribution>
    </vehicle>
</routes>

使結果看起來像這樣:

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
    <vehicle id="0" type="vTypeDist" depart="0.00" departLane="best" departPos="random" departSpeed="random">
        <route edges="bottom7to7/0 7/0to6/0 6/0to5/0 5/0to5/1 5/1to5/2 5/2to6/2"/>
    </vehicle>
</routes>

基本上必須完成以下工作

  • 完全刪除<vtype> (以及其中的<carFollowing...>元素),
  • 刪除<routeDistribution...>
  • 創建<route>元素,它只保留<routeDistribution...>元素中的最后一個edges屬性。

編輯:在這里我使用xml.etree.ElementTree提供我的版本。 為什么所有的downvotes雖然......:/

import xml.etree.ElementTree as ET


if __name__ == "__main__":

tree = ET.parse('total-test.xml')
root = tree.getroot()

# remove <carFollowing> subelement from each vType 
vTypes = root.findall("vType")
for vType in vTypes:
    carFollowings = vType.findall("carFollowing-Krauss")
    for carFollowing in carFollowings:
         vType.remove(carFollowing)

# remove each <vType> (to remove an element reference to its parent is required)
for element in root:
    if element.tag == "vType":
        root.remove(element)

# from root get into <vehicle>
vehicles = root.findall("vehicle")
for vehicle in vehicles:
    # for each <vehicle> get reference <routeDistribution>s
    routeDistributions = vehicle.findall("routeDistribution")
    for routeDist in routeDistributions:
        # for each vehicle distrbution get reference to <route>s
        routes = routeDist.findall("route")

        # fill a container with dictionaries which represent <route> attributes
        listOfRouteDicts = list()
        for route in routes:
            listOfRouteDicts.append(route.attrib)

        # find the min_cost for the given routes
        min_cost = min(float(routeDict['cost']) for routeDict in listOfRouteDicts)
        print(min_cost)

        for route in routes:
            if route.get('cost') == str(min_cost):
                # remove the other attributes of the <route>, we only want the <edges>
                route.attrib = {routeAttr:v for routeAttr,v in route.attrib.items() if routeAttr == "edges"}
                vehicle.append(route)   # move route one level-up to <vehicle> because <routeDistribution> needs to be removed 
            else:
                routeDist.remove(route) # remove all routes which don't have the lowest cost

    # remove the <routeDistribution> for each <vehicle> 
    vehicle.remove(routeDist)
    vehicle.set('type', 'vTypeDist')


tree.write('output.xml')

可能你需要一些更通用的東西。 以下腳本接受您的輸入 (in.xml) 並生成新的輸出 (out.xml)。 當然,這不是很好的編碼,但它可以讓您開始使用語法並幫助您根據需要進行概括。

from xml.dom.minidom import parse, parseString

dom = parse("in.xml" )   # parse an XML file
docRoot = dom.documentElement

# delete all vType
vTypeNode = docRoot.getElementsByTagName('vType')[0]
docRoot.removeChild(vTypeNode)

#i keep only first route node... second is the same... 
#but i am not sure if this will always be the case
routeNode = docRoot.getElementsByTagName('route')[0]

#remove all old route nodes
vehicleNode = docRoot.getElementsByTagName('vehicle')[0]
for child in vehicleNode.childNodes:
    if child.nodeType == child.ELEMENT_NODE:
        vehicleNode.removeChild(child) 

#create a new route node
newRouteNode = dom.createElement("route")
newRouteNode.setAttribute("edges"  , routeNode.getAttribute("edges"))

#append new node
vehicleNode.appendChild(newRouteNode)

#print output
#print dom.toprettyxml()

#write to file
outFile = open("out.xml","wb")
dom.writexml(outFile)
outFile.close()

注意:這只是讓您快速入門!

編輯:

minidom ouptus 總是很臟,因為它包含許多無用的空白。 這是一個眾所周知的問題,但可以通過不同的方式輕松解決。 你可能有興趣看看這里:

使用 toprettyxml() 時新行的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM