-1

I have the following xml file I got from QGIS

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    <Document>
    <name>stationpivot.kml</name>
    <StyleMap id="default0">
        <Pair>
            <key>normal</key>
            <styleUrl>#default</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#hl</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="hl">
        <IconStyle>
            <scale>0.7</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <scale>0.7</scale>
        </LabelStyle>
    </Style>
    <Style id="default">
        <IconStyle>
            <scale>0.7</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <scale>0.7</scale>
        </LabelStyle>
    </Style>
    <Folder>
        <name>stationXML</name>
        <open>1</open>
        <Placemark>
            <name>2</name>
            <Snippet maxLines="0"></Snippet>
            <description><![CDATA[<html><body><table border="1">
<tr><th>Field Name</th><th>Field Value</th></tr>
<tr><td>Latitude</td><td>26.719803</td></tr>
<tr><td>Longitude</td><td>40.861876</td></tr>
<tr><td>Name</td><td>REALNAME2</td></tr>
<tr><td>Vegetation</td><td>v_type2</td></tr>
<tr><td>Description</td><td>text text text text</td></tr>
<tr><td>Time Description</td><td>time time time </td></tr>
</table></body></html>]]></description>
            <styleUrl>#default0</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>40.861876,26.71980299999999,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>3</name>
            <Snippet maxLines="0"></Snippet>
            <description><![CDATA[<html><body><table border="1">
<tr><th>Field Name</th><th>Field Value</th></tr>
<tr><td>Latitude</td><td>46.745151</td></tr>
<tr><td>Longitude</td><td>10.788845</td></tr>
<tr><td>Name</td><td>REALNAME3</td></tr>
<tr><td>Vegetation</td><td>v_type3</td></tr>
<tr><td>Description</td><td>text text text text</td></tr>
<tr><td>Time Description</td><td>time time time</td></tr>
</table></body></html>]]></description>
            <styleUrl>#default0</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>40.788845,26.74515100000001,0</coordinates>
            </Point>
        </Placemark>
      </Folder>
    </Document>
    </kml>

I would like to recursively substitute the value "2" in the

 <name>2</name>
 <name>3</name>

field using the information included in the "description" field REALNAME2 in order to have

<name>REALNAME2</name>
<name>REALNAME3</name>

respectively as final output in my kml

any suggestions?

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
sylar_80
  • 251
  • 3
  • 18

2 Answers2

1

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:

import xml.etree.ElementTree as ET

root = ET.fromstring(<your KML as string>)
name_list = root.findall(".//Placemark/name")
for name in name_list:
    name.text = "Some new text"
Antoine
  • 1,070
  • 7
  • 11
  • I tried it but I always get wrong results! I will really appreciate if you can build an example as soon as you could. – sylar_80 Feb 08 '16 at 16:41
  • @ Antoine I tried to follow your suggestion but I got empty field when using the following code: root_string = ET.fromstring(file_xml) name_list = root_string.findall(".//Placemark/name") print name_list [] print root_string – sylar_80 Feb 09 '16 at 09:50
1

The pykml library can be used to parse existing KML documents and manipulate the KML structures.

Here is example code using pykml that updates the placemark names and creates a new KML file.

from pykml import parser
import re
import lxml.etree as ET

with open('test.kml', 'r') as f:
  doc = parser.parse(f)

for pm in doc.getroot().Document.Folder.Placemark:
  # look for realname in description in markup
  # <tr><td>Name</td><td>*REALNAME2*</td></tr>
  r = re.search(r'<tr><td>Name</td><td>([^<]+)', pm.description.text)
  if r:
    pm.name = r.group(1)

# output new KML file
with open('out.kml', 'wb') as output:
  output.write(ET.tostring(doc, pretty_print=True))
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75