title: Create XML file in Xcode 8 Swift 3
body: <p>I need to create code in Swift for iOS device, to create and save a local XML file. It's actually a KML file with waypoints for exporting to Google earth.
  I found the API reference here: <a href="https://developer.apple.com/reference/foundation/xmldocument" rel="nofollow noreferrer">API Reference - Class - XMLDocument</a>
  But it lacks an example of how to use these functions. Other examples I find online are all in objective C, so I'm lost how to write the code.</p>
  
  <p>If someone can show simple example of how to create the file and add some keys, childs etc, that would be awesome.</p>
  
  <p>An example of what this KML output file will look like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;Document&gt;
      &lt;name&gt;Waypoint Name.kml&lt;/name&gt;
      &lt;StyleMap id="msn_ylw-pushpin"&gt;
          &lt;Pair&gt;
              &lt;key&gt;normal&lt;/key&gt;
              &lt;styleUrl&gt;#sn_ylw-pushpin&lt;/styleUrl&gt;
          &lt;/Pair&gt;
          &lt;Pair&gt;
              &lt;key&gt;highlight&lt;/key&gt;
              &lt;styleUrl&gt;#sh_ylw-pushpin&lt;/styleUrl&gt;
          &lt;/Pair&gt;
      &lt;/StyleMap&gt;
      &lt;Style id="sn_ylw-pushpin"&gt;
          &lt;IconStyle&gt;
              &lt;scale&gt;1.1&lt;/scale&gt;
              &lt;Icon&gt;
                  &lt;href&gt;http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png&lt;/href&gt;
              &lt;/Icon&gt;
              &lt;hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/&gt;
          &lt;/IconStyle&gt;
          &lt;ListStyle&gt;
          &lt;/ListStyle&gt;
      &lt;/Style&gt;
      &lt;Style id="sh_ylw-pushpin"&gt;
          &lt;IconStyle&gt;
              &lt;scale&gt;1.3&lt;/scale&gt;
              &lt;Icon&gt;
                  &lt;href&gt;http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png&lt;/href&gt;
              &lt;/Icon&gt;
              &lt;hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/&gt;
          &lt;/IconStyle&gt;
          &lt;ListStyle&gt;
          &lt;/ListStyle&gt;
      &lt;/Style&gt;
      &lt;Placemark&gt;
          &lt;name&gt;Waypoint Name&lt;/name&gt;
          &lt;description&gt;Description&lt;/description&gt;
          &lt;LookAt&gt;
              &lt;longitude&gt;-114.4379986989632&lt;/longitude&gt;
              &lt;latitude&gt;62.47084160066127&lt;/latitude&gt;
              &lt;altitude&gt;0&lt;/altitude&gt;
              &lt;heading&gt;9.194188445440662e-10&lt;/heading&gt;
              &lt;tilt&gt;0&lt;/tilt&gt;
              &lt;range&gt;1000.000042077875&lt;/range&gt;
              &lt;gx:altitudeMode&gt;relativeToSeaFloor&lt;/gx:altitudeMode&gt;
          &lt;/LookAt&gt;
          &lt;styleUrl&gt;#msn_ylw-pushpin&lt;/styleUrl&gt;
          &lt;Point&gt;
              &lt;gx:drawOrder&gt;1&lt;/gx:drawOrder&gt;
              &lt;coordinates&gt;-114.4435573708376,62.46869255035281,0&lt;/coordinates&gt;
          &lt;/Point&gt;
      &lt;/Placemark&gt;
  &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>