In this php function I append to the node by calling that function in a loop through ajax. The first time the call to appen开发者_Go百科child succeeds. The second time there is a php error "call to undefined method". I don't understand why this happens.
Here is the php function
<?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']));
?>
Here is the XML structure.
<?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>
Try editing like this:
...
if ($xurls->length) {
$xurls = $xurls->item(0); // from DOMNodeList to DOMNode
This is because later you're appending a child to $xurls
, so it must be an element (such as the one returned by createElement()
) instead of a list, like PHP says:
Fatal error: Call to undefined method DOMNodeList::appendChild()
精彩评论