php中的XML解析无法正常工作?

时间:2012-11-09 07:20:06

标签: php mysql xml

我以为我是从mysql查询中在我的php脚本中正确创建了一个xml文档,但我没有得到一个xml文档作为回报(没有php错误来帮助我),即使mysql查询有效!

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

1 个答案:

答案 0 :(得分:2)

$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();
}
相关问题