文档末尾的额外内容 (xml)(php)



我想显示mysql数据库中的所有数据并使用xml格式。但它显示错误消息"第 205 列第 2 行的错误:文档末尾有额外内容"如何解决?

<?php
    extract($_REQUEST);
    include ('connect.php');
    if ($result_no = $conn->query("SELECT * FROM enxml")){
    while ($row_no = $result_no->fetch_object()){
    $xml = '';
    $xml .= '<stationList>';
    $xml .= '<station no="' . $row_no->no . '">' ;
    $xml .= '<location>'. $row_no->location .'</location>';
    $xml .= '<lat>'.$row_no->lat.'</lat>';
    $xml .= '<lng>'.$row_no->lng.'</lng>';
    $xml .= '<type>'.$row_no->type.'</type>';
    $xml .= '<districtL>'.$row_no->districtL.'</districtL>';
    $xml .= '<districtS>'.$row_no->districtS.'</districtS>';
    $xml .= '<address>'.$row_no->address.'</address>';
    $xml .= '<provider>'.$row_no->provider.'</provider>';
    $xml .= '<parkingNo>'.$row_no->parkingNo.'</parkingNo>';
    $xml .= '<img>'.$row_no->img.'</img>';
    $xml .= '</station>';
    $xml .= '</stationList>';

    header("Content-Type:text/xml");
    echo $xml;
    }
    }
    ?>

xml 中应该只有一个根元素。你现在有很多。将<stationList>移出循环while并在循环whileecho xml:

$xml = '';
$xml .= '<stationList>';
while ($row_no = $result_no->fetch_object()){
    $xml .= '<station no="' . $row_no->no . '">' ;
    $xml .= '<location>'. $row_no->location .'</location>';
    $xml .= '<lat>'.$row_no->lat.'</lat>';
    $xml .= '<lng>'.$row_no->lng.'</lng>';
    $xml .= '<type>'.$row_no->type.'</type>';
    $xml .= '<districtL>'.$row_no->districtL.'</districtL>';
    $xml .= '<districtS>'.$row_no->districtS.'</districtS>';
    $xml .= '<address>'.$row_no->address.'</address>';
    $xml .= '<provider>'.$row_no->provider.'</provider>';
    $xml .= '<parkingNo>'.$row_no->parkingNo.'</parkingNo>';
    $xml .= '<img>'.$row_no->img.'</img>';
    $xml .= '</station>';
}
$xml .= '</stationList>';
header("Content-Type:text/xml");
echo $xml;

最新更新