"Only a single root feature allowed within <kml> tag" error with KML code

I have a working KML code displaying GPS'd surveyor markers on property boundary nominally coinciding with a town line. Now I want to do something a little more advanced:

  1. Center the whole earth view over the place in question.

  2. Draw the town line segment (a straight line connecting two corners ~10 km apart (which I had to change to a line connecting points 200 meters apart to stop the line from disappearing in hilly terrain when zooming in), automatically zooming in to a frame representing about 12 km high around the 10 km line, with a viewpoint elevation of 20 km.

  3. Pause a few seconds.

  4. Mark the GPS'd survey markers (using standard icons) and zoom in further to the shorter section of the town line framing my GPS'd markers (LookAt, 3700 meters range)

  5. From this viewpoint, let my users manually navigate, zoom on individual marker icons, click on them for descriptions, etc. etc.

When I try to reorganize my code to add these stages, I get a message: "failed: Only a single root feature allowed within tag".

What am I doing wrong, and how should I be coding this?

Synopsis of code:

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

    <name>MHT-State boundary issues</name>

    <!-- icons for historic stone corner monuments -->

    <Style id="P.Star">
        <IconStyle>
            <Icon>
                <href>
     http://maps.google.com/mapfiles/kml/paddle/purple-stars-lv.png
                </href>
                <scale>0.25</scale>
            </Icon>
        </IconStyle>
    </Style>

    <!-- define blue(boundary) line between corners -->

    <Style id="BlueLine">
      <LineStyle>
        <color>ffff0072</color>
        <width>1</width>
      </LineStyle>
    </Style>

    <!-- locate corners -->

    <Placemark>
      <name>B-H-R corner</name>
      <styleUrl>#P.Star</styleUrl>
      <description>Blandford-Hungtington-Russell corner, coordinates per MassDOT</description>
      <Point><coordinates>-72.872182,42.216453</coordinates></Point>
    </Placemark>

    <Placemark>
      <name>B-G-R corner</name>
      <styleUrl>#P.Star</styleUrl>
      <description>Blandford-Granville-Russell corner, coordinates per MassDOT</description>
      <Point><coordinates>-72.895662,42.123925</coordinates></Point>
    </Placemark>

    <!-- draw town line between them -->

    <Placemark>
      <name>Blandford-Russell boundary</name>
      <styleUrl>#BlueLine</styleUrl>      
      <LineString>
         <tessellate>1</tessellate>
         <altitudeMode>clampToGround</altitudeMode>
         <coordinates>
            -72.895662,42.123925,0
            -72.895622,42.124,0
            ... (more intermediate coordinates in straight line)
            -72.872182,42.216453,0
          </coordinates>          
      </LineString>
    </Placemark>

    <!-- set wide viewpoint to see boundary from Granville to Huntington corners -->

    <LookAt>       
      <longitude>-72.884</longitude>       
      <latitude>42.171</latitude>       
      <range>20000</range>
    </LookAt>

    <!-- pause 5 seconds -->

    <gx:Wait>
      <gx:duration>5</gx:duration>
    </gx:Wait>

    <!-- add legend -->

    <ScreenOverlay>
       <name>Boundary Legend</name>
       <overlayXY x="0" y="0" xunits="fraction" yunits="fraction"/>
       <screenXY x="0" y="0.1" xunits="fraction" yunits="fraction"/>
       <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
       <size x="0" y="0" xunits="fraction" yunits="fraction"/>
       <Icon><href>files\BndyLegend.png</href></Icon>
    </ScreenOverlay>


    <!-- define yellow line for first Hull/DFW boundary segment -->

    <Style id="YellowLine">
      <LineStyle>
        <color>FF14F0FF</color>
        <width>1</width>
      </LineStyle>
    </Style>


    <!-- icon styles
        purple (P.)     - before 1950
        red (R.)        - 1950-1999
        green (G.)      - 2000-2013
        yellow (Y.)     - 2014 on
           white (W.)   - uncertain

        star (.Star)    - end monument
        diamond (.Dmnd) - waypoint stone/concrete monument
        circle (.Cir)   - pipe set in ground, stones or concrete
        square (.Sq)    - blaze
           blank (.blnk)   - blank

    -->


    <!-- icons for historic waypoint stone monuments -->

    <Style id="P.Dmnd">
        <IconStyle>
            <Icon>
                <href>
     http://maps.google.com/mapfiles/kml/paddle/purple-diamond-lv.png
                </href>
                <scale>0.25</scale>
            </Icon>
        </IconStyle>
    </Style>

    ... (more styles)


    <!-- pre-GPS town line marker, Route 23, #2 -->

    <Placemark>
      <name>2</name>
      <styleUrl>#P.Dmnd</styleUrl>
      <description>old town line monument by state route 23, by Garmin eTrex</description>
      <Point><coordinates>-72.88422,42.17023</coordinates></Point>
    </Placemark>

    ... (more point placemarks)

    <!-- set viewpoint directly over #7, from 3700 meters up -->

    <LookAt>       
      <longitude>-72.879562</longitude>       
      <latitude>42.1872075</latitude>       
      <range>3700</range>
    </LookAt>

  </Document>
</kml>
 

What happens:

Initial view of globe, without rotation or zoom appears, then this window pops up:

"Open of file "C:\Users\DTM\Documents\MHT\abutting state parcel\boundary KML\Boundary GPS.kml" failed: Only a single root feature allowed within tag"


Solution 1:

You're missing the <Document> tag near the top of your file. You already have the closing </Document> tag near the end, but seem to be missing the opening one, which should go between the <kml...> tag and the first <name> tag. So your file should have this structure:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document>
        <name>MHT-State boundary issues</name>

        ...

    </Document>
</kml>

For background, every KML must contain only one top-level element inside the <kml>...</kml> tags. That can either be ONE feature (like a Placemark, etc.) or it can be one "container": either a Document or Folder. So if you have a KML that will contain more than one Feature (or Style, etc.), then you need to wrap everything in at least one level of Document or Folder.