为什么调用DOMDocument会失败?

时间:2011-08-23 17:13:36

标签: php xml ajax

在这个PHP脚本中,我从XML文件中获取信息以在HTML页面上创建元素。如果我使用SimpleXMLElement读取XML文件,那么我就能获得信息。我想切换到使用xpath,以便更容易从嵌套节点获取信息。但奇怪的是,创建DOMDocument的调用失败了。这个$dom = new DOMDocument();退出PHP脚本而不返回任何错误,告诉我为什么会发生这种情况。

php脚本是通过页面中javascript的ajax调用的。

这是PHP脚本。

<?php

function get_nodes() {
// load SimpleXML
/* $nodes = new SimpleXMLElement('communities.xml', null, true); */

$dom = new DOMDocument();  // The script fails here
$dom->load('communities.xml');

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = true;

// get document element  

$xpath = new DOMXPath($dom);

$nodes = $xpath->query("//COMMUNITY"); 

$nnodes = sizeof($nodes);
echo "<script type='text/javascript'> alert('Got ".$nnodes." nodes'); </script>";

foreach($nodes as $node) // loop through 
{

        echo "<div id = '".$node['ID']."' class= 'comdiv ui-widget-content' style = 'top: ".$node->TOP."px; left: ".$node->LEFT."px; 

width: ".$node->WIDTH."px; height: ".$node->HEIGHT."px;'> \n";

        /* echo "<script> alert ('<div id = \'".$node['ID']."\' class= \'comdiv ui-widget-content\' style = \'top: ".$node->TOP."; left: 

".$node->LEFT."; width: ".$node->WIDTH."; height: ".$node->HEIGHT.";\'>') </script>";*/

        echo "   <p class = 'comhdr editableText ui-widget-header'>".$node->NAME."</p>\n";

        echo "   <a href='#' onClick=\"delete_div('".$node['ID']."');\">Delete</a>&nbsp;&nbsp;\n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>&nbsp;&nbsp;\n";

        echo "</div> \n";

        echo "<script type='text/javascript'>\n";
        echo "  $('#".$node['ID']."').resizable();\n";
        echo "  $('#".$node['ID']."').draggable();\n";
        echo "  $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');\n";
        echo "</script>\n";


        $nodeid = $node['ID'];

        /* $xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS/URL");

        foreach($xurls as $xurl) {

           echo "a url";

        }*/

}
        echo "<script type='text/javascript'>\n";
        echo "  $('.editableText').editableText();\n";
        echo "</script>\n";

   return;
}

echo get_nodes();

?>

以下是调用脚本的页面上的位置。

<body>

<div id="editdiv" class="comdiv ui-widget-content" style="position: absolute; top: 150px; left: 850px; width:350px; height:250px; border:1px solid grey;">
    <p id="heading" class="comhdr editableText ui-widget-header">Editable</p>
</div>

<script type="text/javascript">

 $(document).ready(function() {
               //define php info and make ajax call to recreate divs from XML data
               $.ajax({
                   url: "get_nodes.php",
                   type: "POST",
                   data: { },
                   cache: false,
                   success: function (response) {

                       if (response != '') 
                       {
                          $(document.body).append(response);

                       }
                   }
               });
 });

</script>

</body>

还有其他的ajax调用,我可以使用DOMDocument加载xml文件。唯一的区别是这个ajax调用来自页面正文。

1 个答案:

答案 0 :(得分:0)

现在您已经发布了错误消息,这是一系列代码:

    echo "  $('#".$node['ID']."').resizable();\n";

必须是$node->getAttribute('ID')