开发者

XML node getelementsbytagname duplicating information

开发者 https://www.devze.com 2023-04-02 18:35 出处:网络
This php function reads an xml file, looks for anode and copies it to anode.If thenode has only one element, the code duplicates that element and writes it twice in thenode.I am wondering why this hap

This php function reads an xml file, looks for a node and copies it to a node. If the node has only one element, the code duplicates that element and writes it twice in the node. I am wondering why this happens.

Here is the PHP file.

<?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']));

Here is the XML file that the php reads to write the backup nodes.

    <?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>15开发者_运维百科0</HEIGHT>
      <URLS>
        <URL ID="u000002">
          <NAME>Facebook.com</NAME>
          <URLC>http://www.facebook.com</URLC>
        </URL>
      </URLS>
    </COMMUNITY>
</COMMUNITIES>

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号