In this PHP script I get information from an XML file to create elements on an HTML page. If I read the XML file with SimpleXMLElement then I am able to get the information. I want to switch to use xpath to make it easier to get information from nested nodes. But strangely enough the call to create a DOMDocument fails. This $dom = new DOMDocument();
exits out of the PHP script without returning any errors to tell me why this happens.
The php script is called through ajax from javascript within the page.
Here is the PHP script.
<?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> \n";
echo " <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a> \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();
?>
Here is the place on the page where the script is called.
<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>
<scr开发者_运维技巧ipt 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>
There are other ajax calls where I am able to load the xml file with DOMDocument. The only difference is that this ajax call is from the body of the page.
Now that you've got the error message posted, it's this series of code:
echo " $('#".$node['ID']."').resizable();\n";
It'd have to be $node->getAttribute('ID')
.
精彩评论