在这个php函数中,我通过ajax循环调用该函数来附加到节点。第一次调用appenchild成功。第二次有一个php错误“调用未定义的方法”。我不明白为什么会这样。
这是php函数
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
function mysql_escape_mimic($inp) {
if(is_array($inp))
return array_map(__METHOD__, $inp);
if(!empty($inp) && is_string($inp)) {
return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
}
return $inp;
}
function add_url( $nodeid, $urlid, $urlname, $urllink ) {
$dom = new DOMDocument();
$dom->load('communities.xml');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = true;
// get document element
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY[@ID='$nodeid']");
if ($nodes->length) {
$node = $nodes->item(0);
$xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS");
if ($xurls->length) {
}
else {
$xurls = $dom->createElement("URLS");
$node->appendChild($xurls);
}
$xurl = $dom->createElement("URL");
$xurl->setAttribute("ID", $urlid);
$xurls->appendChild($xurl); /* Function fails here second time, when node exists already */
$xuname = $dom->createElement("NAME");
$xunameText = $dom->createTextNode(mysql_escape_mimic($urlname));
$xuname->appendChild($xunameText);
$xurl->appendChild($xuname);
$xulink = $dom->createElement("URLC");
$xulinkText = $dom->createTextNode(mysql_escape_mimic($urllink));
$xulink->appendChild($xulinkText);
$xurl->appendChild($xulink);
}
echo "from add_url urlid: ".$urlid." urlname ".$urlname." urllink ".$urllink;
$dom->save('communities.xml');
}
echo add_url(trim($_REQUEST['nodeid']), trim($_REQUEST['urlid']), trim($_REQUEST['urlname']), trim($_REQUEST['urllink']));
?>
这是XML结构。
<?xml version="1.0" encoding="ISO-8859-1"?>
<COMMUNITIES>
<COMMUNITY ID="c000002">
<NAME>ID000002</NAME>
<TOP>192</TOP>
<LEFT>297</LEFT>
<WIDTH>150</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="u000002">
<NAME>Facebook.com</NAME>
<URLC>http://www.facebook.com</URLC>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
答案 0 :(得分:0)
尝试像这样编辑:
...
if ($xurls->length) {
$xurls = $xurls->item(0); // from DOMNodeList to DOMNode
这是因为稍后你将一个孩子附加到$xurls
,所以它必须是一个元素(例如由createElement()
返回的元素)而不是列表,如PHP所说:
致命错误:调用未定义的方法DOMNodeList :: appendChild()