我以为我可以从mysql查询中的php脚本中正确创建xml文档,但是即使mysql查询有效,我也没有得到xml文档作为回报(没有任何php错误对我有帮助)!

    <?php
    ...
    $result = $mysqli->query($sql);
    if ($result) {       //this query works, but no xml document produced as a result
    $d = new DOMDocument();
    $books = $d->createElement('hey');
    $hey->setAttribute('check','The Adventures of Tom Sawyer');
    $d->appendChild($books);
    $d->appendChild($hey);
    $d->appendChild($books);
    echo $d->saveXML();
}
    ?>

最佳答案

$d->setAttribute('check','The Adventures of Tom Sawyer');


$ d是DOMDocument对象,没有DOMDocument :: setAttribute()方法。
使用DOMElement::setAttribute()DOMDocument::createAttribute()

if ($result) {
    $d = new DOMDocument();
    $books = $d->createElement('hey');
    $books->setAttribute('check','The Adventures of Tom Sawyer');
    $d->appendChild($books);
    echo $d->saveXML();
}

09-30 15:29
查看更多