我正在尝试从PHP文件返回XML结构格式......我应该得到一个预期的结果,如:
<points srv="2381" id="1233"><point id="1233" value1="46" value2="11" value3="231" value4="5516" time_evento="2013-11-13 16:54:17" /><point id="1233"...
但是我得到了这个结果的错误:
.../parsererror.xml">XML parse error: illegal...: https://.../file.php <sourcetext><points srv="2381" id="1233"><point id="1233" value1="46" value2="11" value3="231" value4="5516" time_evento="2013-11-13 16:54:17" /><point id="1233"...
即使在文件中我写了结果,它也能正确保存...
以下是代码:
while ($riga = $db->fetch_object($res))
{
$rows_found = true;
if ($riga->srv <> $prev_srv || $riga->id <> $prev_id) {
if (!$bof) {
$xml_document .= '</points>'; // node header
}
$xml_document .= '<points srv="'.$riga->srv.'" id="'.$riga->id.'">';
$bof = false;
$prev_srv = $riga->srv;
$prev_id = $riga->id;
}
$xml_document .= '<point id="'.$riga->id.'" value1="'.$riga->val1.'" value2="'.$riga->val2.'" value3="'.$riga->val3.'" ';
$xml_document .= ' value4="'.$riga->val4.'" ';
$xml_document .= ' time_evento="'.$riga->time_evento.'" />';
}
if ($rows_found) {
$xml_document .= "</points>";
}
$myFile = "/var/tmp/resultXML";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, $xml_document . "\n"); // <---- in the xml_document file I get the correct result
fclose($fh);
// $xml = new SimpleXMLElement($xml_document);
// echo $xml->asXML();
echo $xml_document;
$db->free_results($res);
if(session_id() == '') {
session_write_close();
}
return 0;
有什么建议吗?这让我有点疯狂,三天后我就被困住了......
答案 0 :(得分:0)
想出来:看起来XML文档必须有一个根节点,否则就不会形成良好的形状......如:
<root>
<points ...>
<point.... />
<point.... />
.
.
<point.... />
</points>
<points...>
<point.... />
<point.... />
.
.
<point.... />
</points>
</root>
非常感谢!
干杯, 路易