python - Parsing XML with Pykml -
i have following xml file got 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> i recursively substitute value "2" in
<name>2</name> <name>3</name> field using information included in "description" field realname2 in order have
<name>realname2</name> <name>realname3</name> respectively final output in kml
any suggestions?
i recommend use element tree api xpath. it's quite easy use , powerful. enable want:
import xml.etree.elementtree et root = et.fromstring(<your kml string>) name_list = root.findall(".//placemark/name") name in name_list: name.text = "some new text"
Comments
Post a Comment