XML / PHP文档末尾的额外内容

时间:2013-09-16 20:04:01

标签: php xml document

我的xml中有错误(文档末尾的额外内容),我在互联网上搜索过但没有解析。

我已经阅读了有关根或文档的内容,但确实没有解决我的问题。

PHP:

<?php $doc = new DOMDocument('1.0', 'utf-8');
// we want a nice output
$doc->formatOutput = true;
$query = "SELECT * FROM leden";
while($q = mysql_fetch_assoc($query)) 
{ 
$user = $doc->createElement('user');
$user = $doc->appendChild($user);
$title = $doc->createElement('id');
$title = $user->appendChild($title);
$text = $doc->createTextNode($q['id']);
$text = $title->appendChild($text);
$title = $doc->createElement('nickname');
$title = $user->appendChild($title);
$text = $doc->createTextNode($q['gebruikersnaam']);
$text = $title->appendChild($text);
//etc.. etc
}
echo $doc->saveXML($doc);
?>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>300</id>
<nickname>Peterjhonsen</nickname>
<url>http://www.abcd.nl/leden/peterjhons&amp;refid=123&amp;cp=gg</url>
<foto>http://www.abcd.nl/image.php/uploads/2013043003560sdfsdfl2340.jpg?width=100&amp;height=100&amp;cropratio=1:1&amp;image=/uploads/20130430sdfsdf2340.jpg</foto>
<leeftijd>55</leeftijd>
<geslacht>man</geslacht>
<postuur>Vol</postuur>
<provincie>Belgie</provincie>
<ras>Blank</ras>
<profieltext>Lorem ipsum</profieltext>
<refid>530</refid>
</user>
<user>
<id>422</id>
<nickname>Jackson</nickname>
<url>http://www.abcd.nl/leden/timmerman&amp;refid=530&amp;cp=gg</url>
<foto>http://www.abcd.nl/image.php/uploads/25201842tidsfsrman.jpg?width=100&amp;height=100&amp;cropratio=1:1&amp;image=/uploads/25201842sdfsdf.jpg</foto>
<leeftijd>27</leeftijd>
<geslacht>man</geslacht>
<postuur>Normaal</postuur>
<provincie>Noord-Brabant</provincie>
<ras>Blank</ras>
<profieltext></profieltext>
<refid>530</refid>
</user>

1 个答案:

答案 0 :(得分:0)

您的所有用户都需要父级,例如:

<?xml version="1.0" encoding="UTF-8"?>
<userlist>
    <user>
        <id>300</id>
        <nickname>Peterjhonsen</nickname>
    </user>
    <user>
        <id>400</id>
        <nickname>Fred</nickname>
    </user>
</userlist>

只需在第1行后插入:

$root = $doc->createElement('userlist');
$root = $doc->appendChild($root);

并相应地更改while之后的代码:

...
$user = $root->appendChild($user);