【问题标题】:XML / PHP Extra content at the end of the document文档末尾的 XML/PHP 额外内容
【发布时间】:2013-09-17 06:03:08
【问题描述】:

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

【问题讨论】:

    标签: php xml document


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      相关资源
      最近更新 更多