I'm trying to edit a line in an XML file. One of the XML elements, called polyline, contains coordinates:
<location>
<street>Interstate 76 (Ohio to Valley Forge)</street>
<direction>ONE_DIRECTION</direction>
<location_description>Donegal, PA</location_description>
<polyline>40.100045 -79.202435, 40.09966 -79.20235, 40.09938 -79.20231<polyline>
</location>
I need to reverse the order and add a comma between each coordinate so it is written as:
<polyline>-79.202435,40.100045,-79.20235,40.09966,-79.20231,40.09938<polyline>
I can parse the file and format the polyline element, but not sure how to write it back into the XML file:
from xml.dom import minidom
mydoc = minidom.parse(xmlFile)
items = mydoc.getElementsByTagName('polyline')
for item in items:
newPolyline = []
lineList = item.firstChild.data.split(",")
for line in lineList:
lon = line.split(" -")[1]
lat = line.split(" -")[0]
newPolyline.append(str(lon))
newPolyline.append(str(lat))