XML节点getelementsbytagname重复信息

发布于 2024-12-03 01:24:59 字数 6357 浏览 0 评论 0原文

该 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>

谢谢。

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>150</HEIGHT>
      <URLS>
        <URL ID="u000002">
          <NAME>Facebook.com</NAME>
          <URLC>http://www.facebook.com</URLC>
        </URL>
      </URLS>
    </COMMUNITY>
</COMMUNITIES>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文