I have the following xml file I got from QGIS
JavaScript
x
79
79
1
<?xml version="1.0" encoding="UTF-8"?>
2
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
3
<Document>
4
<name>stationpivot.kml</name>
5
<StyleMap id="default0">
6
<Pair>
7
<key>normal</key>
8
<styleUrl>#default</styleUrl>
9
</Pair>
10
<Pair>
11
<key>highlight</key>
12
<styleUrl>#hl</styleUrl>
13
</Pair>
14
</StyleMap>
15
<Style id="hl">
16
<IconStyle>
17
<scale>0.7</scale>
18
<Icon>
19
<href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png</href>
20
</Icon>
21
</IconStyle>
22
<LabelStyle>
23
<scale>0.7</scale>
24
</LabelStyle>
25
</Style>
26
<Style id="default">
27
<IconStyle>
28
<scale>0.7</scale>
29
<Icon>
30
<href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
31
</Icon>
32
</IconStyle>
33
<LabelStyle>
34
<scale>0.7</scale>
35
</LabelStyle>
36
</Style>
37
<Folder>
38
<name>stationXML</name>
39
<open>1</open>
40
<Placemark>
41
<name>2</name>
42
<Snippet maxLines="0"></Snippet>
43
<description><![CDATA[<html><body><table border="1">
44
<tr><th>Field Name</th><th>Field Value</th></tr>
45
<tr><td>Latitude</td><td>26.719803</td></tr>
46
<tr><td>Longitude</td><td>40.861876</td></tr>
47
<tr><td>Name</td><td>REALNAME2</td></tr>
48
<tr><td>Vegetation</td><td>v_type2</td></tr>
49
<tr><td>Description</td><td>text text text text</td></tr>
50
<tr><td>Time Description</td><td>time time time </td></tr>
51
</table></body></html>]]></description>
52
<styleUrl>#default0</styleUrl>
53
<Point>
54
<gx:drawOrder>1</gx:drawOrder>
55
<coordinates>40.861876,26.71980299999999,0</coordinates>
56
</Point>
57
</Placemark>
58
<Placemark>
59
<name>3</name>
60
<Snippet maxLines="0"></Snippet>
61
<description><![CDATA[<html><body><table border="1">
62
<tr><th>Field Name</th><th>Field Value</th></tr>
63
<tr><td>Latitude</td><td>46.745151</td></tr>
64
<tr><td>Longitude</td><td>10.788845</td></tr>
65
<tr><td>Name</td><td>REALNAME3</td></tr>
66
<tr><td>Vegetation</td><td>v_type3</td></tr>
67
<tr><td>Description</td><td>text text text text</td></tr>
68
<tr><td>Time Description</td><td>time time time</td></tr>
69
</table></body></html>]]></description>
70
<styleUrl>#default0</styleUrl>
71
<Point>
72
<gx:drawOrder>1</gx:drawOrder>
73
<coordinates>40.788845,26.74515100000001,0</coordinates>
74
</Point>
75
</Placemark>
76
</Folder>
77
</Document>
78
</kml>
79
I would like to recursively substitute the value “2” in the
JavaScript
1
3
1
<name>2</name>
2
<name>3</name>
3
field using the information included in the “description” field REALNAME2 in order to have
JavaScript
1
3
1
<name>REALNAME2</name>
2
<name>REALNAME3</name>
3
respectively as final output in my kml
any suggestions?
Advertisement
Answer
I recommend you to use the element tree API together with XPath. It’s quite easy to use and very powerful. It will enable you to do what you want:
JavaScript
1
7
1
import xml.etree.ElementTree as ET
2
3
root = ET.fromstring(<your KML as string>)
4
name_list = root.findall(".//Placemark/name")
5
for name in name_list:
6
name.text = "Some new text"
7