XML解析错误:使用DomDocument时格式不正确

I am currently following this:

https://developers.google.com/maps/articles/phpsqlajax_v3#domfunctions

Which has lead me with this code:

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);


// Select all the rows in the markers table
$query = "SELECT latitude, longitude FROM people";
$result = $db->query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = $result->fetch_assoc()){
    $lat = $row["latitude"];
    $lng = $row["longitude"];
    echo $lat;
    echo $lng;
    // ADD TO XML DOCUMENT NODE
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("lat", $row['latitude']);
    $newnode->setAttribute("lng", $row['longitude']);

}

$xmlfile = $dom->saveXML();
echo $xmlfile;
?>

However this gives me the following error.

XML Parsing Error: not well-formed Location: myfile Line Number 1, Column 35:53.554825-6.7911150.0000000.000000

If I remove the header() line, then I get the latitude and longitudes in browser. What modification needs to be made to make the xml well formed to work?

<?php

    $query = "SELECT latitude, longitude FROM people";
    $result = $db->query($query);
    $fields = $result->fetch_fields();

    if( $result ) {

        $dom = new DOMDocument('1.0','utf-8');
        $node = $dom->createElement("markers");
        $parnode = $dom->appendChild($node);

        while( $row = $result->fetch_assoc() ){
            $node = $dom->createElement("marker");
            $newnode = $parnode->appendChild($node);
            /* Add attribute for any/all fields in recordset dynamically */
            foreach( $fields as $field ) $newnode->setAttribute( $field->name, $row[ $field->name ] );
        }

        header("Content-type: text/xml");
        exit( $dom->saveXML() );
    }
    exit('error');
?>