1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // This program makes some basic use of the KML Engine KmlFile class.
27 // KmlFile's GetObjectById() provides an efficient and convenient means to fine
28 // an element by its XML ID.
29 
30 #include <iostream>
31 #include "kml/dom.h"
32 #include "kml/engine.h"
33 
HelloKmlFile()34 void HelloKmlFile() {
35   const std::string kKml =
36     "<kml xmlns=\"http://www.opengis.net/kml/2.2\">"
37       "<Folder id=\"f0\">"
38         "<name>Folder 0</name>"
39         "<Placemark id=\"pm0\">"
40           "<name>Placemark 0</name>"
41         "</Placemark>"
42         "<Placemark id=\"pm1\">"
43           "<name>Placemark 1</name>"
44         "</Placemark>"
45       "</Folder>"
46     "</kml>";
47 
48   // Parsing into a KmlFile creates an internal database of object id mappings.
49   kmlengine::KmlFilePtr kml_file = kmlengine::KmlFile::CreateFromParse(kKml,
50                                                                        NULL);
51 
52   // Access the 3 Features by their XML Id.  Note that GetObjectById() returns
53   // a ObjectPtr, hence the use of the cast to access <name>.
54   kmldom::FolderPtr folder0 = AsFolder(kml_file->GetObjectById("f0"));
55   std::cout << "Folder 0 name: " << folder0->get_name() << std::endl;
56   kmldom::PlacemarkPtr placemark0 = AsPlacemark(kml_file->GetObjectById("pm0"));
57   std::cout << "Placemark 0 name: " << placemark0->get_name() << std::endl;
58   kmldom::PlacemarkPtr placemark1 = AsPlacemark(kml_file->GetObjectById("pm1"));
59   std::cout << "Placemark 1 name: " << placemark1->get_name() << std::endl;
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63   HelloKmlFile();
64   return 0;
65 }
66