Building XML using ElementTree and Minidom

Most of the time, XML can be built by concatenating strings, as you can see in the following command:

xml = "<?xml version="1.0" encoding="utf-8"?>"xml += "<kml xmlns="http://www.opengis.net/kml/2.2">"xml += " <Placemark>"xml += " <name>Office</name>"xml += " <description>Office Building</description>"xml += " <Point>"xml += " <coordinates>"xml += " -122.087461,37.422069"xml += " </coordinates>"xml += " </Point>"xml += " </Placemark>"xml += "</kml>"

However, this method can be quite prone to typos, which creates invalid XML documents. A safer way is to use an XML library. Let's build this simple KML document using ElementTree:

  1. We'll define the rootKML element and assign it a namespace.
  2. Then, we'll ...

Get Learning Geospatial Analysis with Python - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.