在這個網頁上,我有一個PHP腳本來重新創建基於XML數據的頁面上的div。該腳本直接從<body> </body>
內的HTML頁面調用。XML的SimpleXMLElement返回節點,DOMXPath返回錯誤
<body>
<div id="page" class="demo">
</div>
<?php
// script for recreating divs calls SimpleXMLElement or DOMXPath
?>
</body>
當我用SimpleXMLElement得到XML節點時,沒有錯誤,節點被返回。當我嘗試使用DOMXPath時,會出現錯誤。
這將返回的節點。
$nodes = new SimpleXMLElement('communities.xml', null, true);
foreach($nodes as $node) // loop through
{
//restore divs from node information
}
這不會返回節點並給出錯誤。
$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
}
擴展腳本以從我認爲使用xpath的節點獲取更多信息。我想要顯示的附加信息是:NAME:顯示div(Google.com)中包含的每個網址的文本,URLC:與文本(http://google.com)相對應的網址。 XML文件具有這樣的節點。
<?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>
奇怪的問題是,在其他地方,從JavaScript函數Ajax調用我從使用XPath的XML文件的信息,沒有任何問題。爲什麼在這種情況下DOMXPath方法不起作用?
EDITS *新信息
這裏是通過從HTML頁面的Ajax調用PHP文件。這是SimpleXMLElement的作用。但是如果我嘗試創建新的DOMdocument()
,則請調用xpath。新的DOM文檔不會被創建。
<?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 " $('#".$node['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();
?>
你會得到什麼錯誤? – Gordon
奇怪的是現在沒有錯誤,但DOMXPath似乎沒有找到節點。 – user823527