duanfang5849 2015-12-10 10:10
浏览 55

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?

  • 写回答

1条回答 默认 最新

  • dongxin0031 2015-12-10 10:33
    关注
    <?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');
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起