In this web page, I have a php script to recreate divs on the page based on XML data. The script is called directly from the HTML page inside <body> </body>
.
<body>
<div id="page" class="demo">
</div>
<?php
// script for recreating divs calls SimpleXMLElement or DOMXPath
?>
</body>
When I get the XML nodes with SimpleXMLElement, there are no errors, the nodes are returned. When instead I try DOMXPath, there are errors.
This returns the nodes.
$nodes = new SimpleXMLElement('communities.xml', null, true);
foreach($nodes as $node) // loop through
{
//restore divs from node information
}
This doesn't return the nodes and gives errors.
$dom = new DOMDocument();
$dom->load('communities.xml');
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('COMMUNITY');
foreach($nodes as $node) // loop through
{
//restore divs from node information
}
To extend the script to get more information from the nodes I thought of using xpath. The additional information I want to show is: NAME: text to show for each url contained in the div (Google.com), URLC: url corresponding to the text (http://google.com). The XML file has nodes like this.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<COMMUNITIES>
<COMMUNITY ID="c000010">
<NAME>New000010</NAME>
<TOP>50</TOP>
<LEFT>50</LEFT>
<WIDTH>150</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="u000038">
<NAME>Google.com</NAME>
<URLC>http://google.com</URLC>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
The strange problem is that elsewhere, in ajax calls from javascript functions I get information from the XML file with xpath without any problems. Why doesn't the DOMXPath method work in that situation?
EDITS * NEW INFORMATION
Here is the php file called through ajax from the html page. This is where SimpleXMLElement works. But if I try to create a new DOMdocument()
then call xpath. The new DOM document isn't created.
<?php
function get_nodes() {
// load SimpleXML
/* $nodes = new SimpleXMLElement('communities.xml', null, true); */
$dom = new DOMDocument();
$dom->load('communities.xml');
// get document element
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY");
foreach($nodes as $node) // loop through
{
echo "<div id = '".$node['ID']."' class= 'comdiv ui-widget-content' style = 'top: ".$node->TOP."; left: ".$node->LEFT."; width: ".$node->WIDTH.";
height: ".$node->HEIGHT.";'> \n"; 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 " $('#".$nod开发者_开发问答e['ID']."').draggable('option', 'handle', '.comhdr');\n";
echo "</script>\n";
}
echo "<script type='text/javascript'>\n";
echo " $('.comhdr').editableText();\n";
echo "</script>\n";
return;
}
echo get_nodes();
?>
Community is not the root of the xml. Try $nodes = $xpath->query('//COMMUNITY');
This should select any COMMUNITY nodes in your xml document. If you only want to select them when they're children of the root element, then try:
$nodes = $xpath->query('/COMMUNITIES/COMMUNITY');
Had folder permission issues. The folder containing the XML did not have permission set for the script to write the updated XML.
精彩评论