Skip to content
Advertisement

Parsing XML with Pykml

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?

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:

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"
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement