Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

“Hello, Earth”

A standard placemark in Google Earth uses a yellow pushpin icon to point to a particular spot on the Earth’s surface. A placemark usually has a name that identifies the location. It’s a good practice to include a description as well. The description is displayed by web search results and will help users decide if they want to view your KML files.

The following KML example creates a simple placemark with the name “Hello, Earth.” The description provides additional information about this place (Figure 1-7).

Figure 1-7

Figure 1-7 Anatomy of a placemark. You will usually create a name and a description for a placemark. If you have a lot to say, description balloons provide related text, images, and links to other places on the web.

listingicon.jpg HelloEarth.kml

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
    <name>Hello, Earth</name>
    <description>Here's where we developed Google Earth!</description>

Viewing the Examples in This Book

To view this example in Google Earth, first download and install Google Earth. You can obtain a free copy of Google Earth from the Google website http://earth.google.com. A complete listing of examples is provided on this book’s website at www.informit.com/title/9780321525598. Click the title of any example to view it in Google Earth.


If you’d like some hands-on experience, you can enter this example text into any basic text editor that saves text without adding any formatting information, such as Notepad. Save the file with a filename that ends in .kml and open the file in Google Earth on your computer. Once you’ve saved the file in this manner, you can also edit it. For example, try changing the <name>, save the file again, and then open it in Google Earth. Next, try modifying the values for the <coordinates> element, save the file, and watch where the new placemark appears.

The best way to learn KML is to experiment with sample files, changing values and viewing the results in your favorite Earth browser. If you make a mistake, you may not see anything in the browser, but that’s your clue that something’s amiss. Google Earth provides a feature for error checking that you may find helpful. (Select Options > General, and under the heading KML Error Handling, select Show Prompts for All Errors.) You can also use a KML validator to check your KML code. For example, see the KML validator by Galdos Systems at www.kmlvalidator.com.

Structure of a KML File

Every KML file begins with the two lines shown in this example.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">

If you’re creating a KML file from scratch, be sure to copy these two lines verbatim into the beginning of the file. A KML file can contain only one <kml> element. Don’t forget the closing </kml> tag at the end of the file.

The file contains a <Placemark> element that has three children. The angled brackets < > indicate KML element names:

Children of <Placemark>


Label for the placemark.


Text (and optional images) providing additional information about the placemark. The <description> appears in the information balloon. This balloon pops up when the user clicks the placemark name in the Places panel or the placemark icon in the 3D viewer of Google Earth).


Contains the <coordinates> element. The <coordinates> element contains values for the longitude, latitude, and altitude of the <Placemark>. See the section in Chapter 3 on “Coordinates” for more detail.

Figure 1-7 shows how the name and the description appear in both the 3D viewer and the Places panel of Google Earth.

Because KML is an XML data format, it has a consistent structure that observes certain patterns. An element begins with its name in angled brackets (<Placemark>). An element ends with an angled bracket and a slash preceding the element name (</Placemark>). The element’s value is contained within these delimiters.

Definition of Simple/Complex Elements

In KML, any word contained in angled brackets < > is an element. When an element name begins with a capital letter, it is a complex element, which means that it can contain other elements. For example, in this code excerpt, <Point> is a complex element that contains the <coordinates> element:


Names of simple elements begin with a lowercase letter. Simple elements cannot contain other elements. A simple element contains only character data (in XML terms: letters, digits, and symbols that are not used for XML markup purposes). In the HelloEarth.kml example, <name>, <description>, and <coordinates> are examples of simple elements.

Complex elements are also called parents because they contain other elements. Simple elements are called children. In a KML file, the children are indented several spaces from their parent’s position in the file, but this convention is simply for readability. The Earth browser does not pay attention to the different levels of indentation (white space).

General Rules in KML

Here are some general rules to keep in mind when authoring KML files:

  • Case is significant. Each element name must be spelled exactly as shown in the KML 2.2 Reference, and with the same capitalization (see Appendix A).
  • Order is significant. KML child elements must be listed in the same order as listed within their parent element in the KML 2.2 Reference. You can omit child elements, but you cannot rearrange them.
  • Child elements can belong only to the allowed parent elements. Again, if you follow the ordering within the individual syntax sections in the KML 2.2 Reference, you’ll be doing things correctly.
  • + Share This
  • 🔖 Save To Your Account