donglian4770 2012-01-30 23:04
浏览 103

DOM返回空对象

This is my code:

<?php
$content = utf8_encode( file_get_contents( "data.xml") );
$XML = new DOMDocument();
$XML->loadXML($content);

echo "<pre>" . print_r($XML, 1) . "</pre>";
?>

It does return:

 DOMDocument Object
(
)

I've read its a bug or something, since both var_dump and print_r or any will return an empty object element.

http://www.php.net/manual/en/class.domelement.php#86224

This guy gave an solution to better use:

$xml = $XML->saveXML($XML);
echo "<pre>" . print_r($xml, 1) . "</pre>";

But then, when I'm reloading the page, it looks like there are only spaces, because I can't see anything, but I can select it...

Where is the problem, have you guys any solutions for that?

  • 写回答

3条回答 默认 最新

  • doulu5109 2012-08-21 19:03
    关注

    A var_dump on DOMDocument always returns an empty object.

    Illustration:

    $xml = simplexml_load_file('test.xml'); //load your xml
    
    var_dump($xml); //var_dump via SimpleXML (lot of data)
    
    $doc = new DOMDocument(); //fire up domdocument
    
    $xmlstring = $xml->asXML(); //convert SimpleXML to string
    
    $doc->loadXML($xmlstring);  //loads perfectly 
    
    var_dump($doc); //empty object
    

    Maybe you should consider SimpleXML?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作