Parsing XML with Pykml

2024/11/18 11:45:08

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?

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 ETroot = ET.fromstring(<your KML as string>)
name_list = root.findall(".//Placemark/name")
for name in name_list:name.text = "Some new text"
https://en.xdnf.cn/q/118684.html

Related Q&A

Python Google App Engine Receiving a string in stead of JSON object

I am sending a HTTP POST request from android to a server using the script belowURI website = new URI("http://venkygcm.appspot.com");HttpClient client = new DefaultHttpClient();HttpPost reque…

Creating Gui for python client server

Help needed with my python project. Unable to get the code for the client or server implemented with the Gui i created. it is one based on book seller where the client is the buyer and the server is th…

Maximum Subarray sum - Where is my solution wrong? Kadanes Algorithm

Here is a description of the problem:The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: max_sequence([-2, 1, -3, 4, -1, 2,…

Pandas: Selecting rows and columns based on a subset of columns that contain a certain value

Lets say I have a dataframe with column names as follows:col_id_1, col_id_2, ..., col_id_m, property_1, property_2 ..., property_nAs an example, how would I search across all col_ids for, say, the valu…

Open a image file then display it in new window

I have a button in my GUI then after selecting a image file I want it to display in new window.Code:import tkinter as tk from tkinter import * from tkinter import ttk from tkinter import filedialog …

python plotting chart in interactive viewer vscode

This works and shows a plot in vscode: #%% cell with plot import matplotlib.pyplot as plt y = [3.2, 3.9, 3.7, 3.5, 3.02199] x = [0.15, 0.3, 0.45, 0.6, 0.75] n = [155, "outliner", 293, 230, 67…

Find all lines in a dataframe that matches specific pattern and extract the strings after split

I have a dataframe that looks like LineEntry: [0x0000000002758261-0x0000000002758268): /a/b/c:7921:14 LineEntry: [0x0000000002756945-0x0000000002756960): /f/b/c:6545:10 LineEntry: [0x00000000027562c9-0…

Python: Concatenate many dicts of numpy arrays with same keys and size

I have a function called within a loop that returns a dict (dsst_mean) with roughly 50 variables. All variables are numpy arrays of length 10.The loop iterates roughly 3000 times. Im current concatenat…

intersection of 2 objects of different types

i want to take the intersection of: ((e, 13.02338360095244), (a, 11.820318700775383), (o, 9.20172171683253), (s, 7.635081506807498), (n, 7.547469320471335), (i, 7.219915745772025), (r, 6.70492704072287…

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…