2017-04-05 118 views
0

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

<?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; 
    } 
    } 

    ?> 

回答

0

xml中应该只有一个根元素。你现在有很多。移动<stationList>while环和echo XML后while循环:

$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; 
+0

太感谢你了! – HTT

+0

,因为我用这种方法可以显示所有JSON格式的数据... – HTT

+0

很高兴为您提供帮助。如果答案可以帮助你 - 请接受它。接受利润的一些解释 - https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work同时考虑接受你以前的问题中的答案。 –

相关问题