0

我的 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>
4

1 回答 1

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);
于 2013-09-16T22:03:08.490 回答