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:
- We'll define the rootKML element and assign it a namespace.
- Then, we'll ...