此php函数读取xml文件,查找节点并将其复制到节点。如果节点只有一个元素,则代码会复制该元素并在节点中将其写入两次。我想知道为什么会这样。
这是PHP文件。
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
function backup_urls( $nodeid ) {
$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) {
$xurle = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS_BACKUP");
echo "backup length: ".$xurle->length;
if ($xurle->length) $node->removeChild($xurle->item(0));
$xurlb = $dom->createElement("URLS_BACKUP");
$node->appendChild($xurlb);
$urldelete = $xurls->item(0);
$xurll = $urldelete->getElementsByTagName ("URL");
foreach($xurll as $url)
{
$urlid = $url->getAttribute('ID');
$xurl = $dom->createElement("URL");
$xurl->setAttribute("ID", $urlid);
$xurlb->appendChild($xurl);
$name = $url->getElementsByTagName("NAME")->item(0)->nodeValue;
$xuname = $dom->createElement("NAME");
$xunameText = $dom->createTextNode($name);
$xuname->appendChild($xunameText);
$xurl->appendChild($xuname);
$urllink = $url->getElementsByTagName("URLC")->item(0)->nodeValue;
$xulink = $dom->createElement("URLC");
$xulinkText = $dom->createTextNode($urllink);
$xulink->appendChild($xulinkText);
$xurl->appendChild($xulink);
echo "urlid: ".$urlid." name: ".$name." link: ".$urllink;
}
$node->removeChild($urldelete);
}
}
$dom->save('communities.xml');
}
echo backup_urls(trim($_REQUEST['nodeid']));
这是php用来编写备份节点的XML文件。
<?xml version="1.0" encoding="ISO-8859-1"?>
<COMMUNITIES>
<COMMUNITY ID="c001">
<NAME>Town Services</NAME>
<TOP>41</TOP>
<LEFT>52</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Google.com</NAME>
<URLC>http://www.google.com</URLC>
</URL>
<URL ID="U002">
<NAME>Bing.com</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
<URL ID="U003">
<NAME>Yahoo.com</NAME>
<URLC>http://www.yahoo.com</URLC>
</URL>
<URL ID="U004">
<NAME>Aol.com</NAME>
<URLC>http://www.aol.com</URLC>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c002">
<NAME>Local Stores</NAME>
<TOP>27</TOP>
<LEFT>270</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Walgreens</NAME>
<URLC>http://www.walgreens.com</URLC>
</URL>
<URL ID="U002">
<NAME>Bing.com</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
<URL ID="U003">
<NAME>Yahoo.com</NAME>
<URLC>http://www.yahoo.com</URLC>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c003">
<NAME>Attractions</NAME>
<TOP>50</TOP>
<LEFT>490</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Museum</NAME>
<URLC>http://www.mfa.org</URLC>
</URL>
<URL ID="U002">
<NAME>Park</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c006">
<NAME>Travel</NAME>
<TOP>370</TOP>
<LEFT>490</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>200</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Southwest</NAME>
<URLC>http://www.mfa.org</URLC>
</URL>
<URL ID="U002">
<NAME>Northwest</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c000010">
<NAME>New000010</NAME>
<TOP>372</TOP>
<LEFT>54</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="u000038">
<NAME>Google.com</NAME>
<URLC>http://google.com</URLC></URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c000011">
<NAME>N000011</NAME>
<TOP>370</TOP>
<LEFT>295</LEFT>
<WIDTH>150</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="u000040">
<NAME>AOL.com</NAME>
<URLC>http://www.aol.com</URLC>
</URL>
</URLS>
</COMMUNITY>
<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>
感谢。