为什么count(DOMNode-> childNodes)返回的子项数不正确? (PHP和XML)

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了为什么count(DOMNode-> childNodes)返回的子项数不正确? (PHP和XML)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个名为PRoceSSDeleteForm()的 PHP函数,用于从名为structure.XMl的XML文档删除指定的节点.到目前为止我遇到的唯一问题是在ProcessDeleteForm()中,它通过循环遍历父节点的每个子节点来搜索删除的节点,并通过“name”属性匹配它,但是我没有能够为循环获取正确数量的子节点,因此它在到达正确的节点之前就会停止.相关代码

function processDeleteForm($dir,$filename)
{
  echo "Processing delete request.<br/>";
  echo "Request to delete ".$filename." From ".$dir.".<br/>";
  $xMLDoc = new DOMDocument();

  $xmlDoc->load("structure.xml");
  $node = dirDOMNodeWrITable($dir,$xmlDoc);

  $target;
  echo "Working directory has ".count($node->childNodes)." child(ren).<br/>";
  for($x = 0; $x < count($node->childNodes); $x++)
  {
    if($node->childNodes->item($x)->getAttribute("name") == $filename)
    {
      $target = $node->childNodes->item($x);
      echo "Target found.<br/>";
    }
    else
    {
      echo "SeArching for target...<br/>";
    }
  }

  if($target->getAttribute("tyPE") != "directory")
  {
    $fStored = "uploads/".$target->childNodes->item(0)->wholeText;
    unlink($fstored);
  }

  $node->removeChild($target);

  $file = fopen("structure.xml","w");
  fwrite($file,$xmlDoc->saveXML());

}

structure.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<directory name="root" owner="elarsen" read="aclum,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" write="aclum,tperkins" type="directory">
  <directory name="music" owner="elarsen" read="aclum,tperkins" type="directory">
    <directory name="She" owner="elarsen" read="aclum,tperkins" type="directory">
      <directory name="Chiptek" owner="elarsen" read="aclum,tperkins" type="directory">
        <directory name="mP3" owner="elarsen" read="aclum,tperkins" type="directory">
          <file name="intro.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">intro.mp3</file>
          <file name="music.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">;music.mp3</file>
          <file name="supersonic.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">supersonic.mp3</file>
          <file name="memories.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">memories.mp3</file>
          <file name="chiptek.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">chiptek.mp3</file>
          <file name="intermission.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">intermission.mp3</file>
          <file name="kicks.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">kicks.mp3</file>
          <file name="1997.mp3" owner="elarsen" read="aclum,tperkins" type="audio/mpeg">1997.mp3</file>
        </directory>
        <directory name="ogg" owner="elarsen" read="aclum,tperkins" type="directory">
          <file name="intro.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">intro.ogg</file>
          <file name="music.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">music.ogg</file>
          <file name="supersonic.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">supersonic.ogg</file>
          <file name="memories.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">memories.ogg</file>
          <file name="chiptek.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">chiptek.ogg</file>
          <file name="intermission.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">intermission.ogg</file>
          <file name="kicks.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">kicks.ogg</file>
          <file name="1997.ogg" owner="elarsen" read="aclum,tperkins" type="audio/ogg">1997.ogg</file>
        </directory>
      </directory>
    </directory>
  </directory>
  <directory name="test01" owner="elarsen" read="0" write="0" type="directory"/>
  <directory name="test02" owner="elarsen" read="0" write="0" type="directory"/>
  <directory name="test03" owner="elarsen" read="0" write="0" type="directory"/>
</directory>

*以上应该是xml文件内容,但我无法弄清楚如何显示它.如果有更多关于在stackoverflow上格式化xml的知识的人可以解决它,我会非常感激.

查看负责创建名为“test01”,“test02”和“test03”的目录的代码可能也很有用:

function processNewForm($dir,$dirName,$readPrivs,$writePrivs)
{
  $readString = "";
  $writeString = "";
  $dirOwner = $_SESSION["user"];

  for($x = 0; $x < count($readPrivs); $x++)
  {
    $readString += $readPrivs[$x].",";
  }
  for($x = 0; $x < count($writePrivs); $x++)
  {
    $writeString += $writePrivs[$x].",";
  }

  $xmlDoc = new DOMDocument();

  $xmlDoc->load("structure.xml");
  $node = dirDOMNodeWritable($dir,$xmlDoc);

  $newDir = $xmlDoc->createElement("directory");
  $newDir->setattribute("name",$dirName);
  $newDir->setAttribute("owner",$dirOwner);
  $newDir->setAttribute("read",$readString);
  $newDir->setAttribute("write",$writeString);
  $newDir->setAttribute("type","directory");

  $node->appendChild($newDir);

  $file = fopen("structure.xml",$xmlDoc->saveXML());

}

当前@L_406_32@如下所示:

Received delete request.
Validating delete request... Request is valid.
Processing delete request.
Request to delete test03/ from /.
Working directory has 1 child(ren).
Searching for target...

Notice: Undefined VARiable: target in E:\aepi\dev\fileshare.PHP on line 540

Fatal error: Call to a member function getAttribute() on a non-object in E:\aepi\dev\fileshare.PHP on line 540

解决方法

PHP manual for count()

DOMNode::$childNodesDOMNodeList对象.它不是Countable.它总会返回一个(即使是空的).请改用其@L_126_42@财产:

$count = $element->childNodes->length;

更新:从PHP 7.2起,domnodelist已成为可数.

脚本宝典总结

以上是脚本宝典为你收集整理的为什么count(DOMNode-> childNodes)返回的子项数不正确? (PHP和XML)全部内容,希望文章能够帮你解决为什么count(DOMNode-> childNodes)返回的子项数不正确? (PHP和XML)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。