加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:08:39 所属栏目:PHP教程 来源:网络整理
导读:我有一个名为processDeleteForm()的 PHP函数,用于从名为structure.xml的xml文档中删除指定的节点.到目前为止我遇到的唯一问题是在processDeleteForm()中,它通过循环遍历父节点的每个子节点来搜索要删除的节点,并通过“name”属性匹配它,但是我没有能够为循环
我有一个名为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());

}

当前输出如下所示:

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:aepidevfileshare.php on line 540

Fatal error: Call to a member function getAttribute() on a non-object in E:aepidevfileshare.php on line 540

解决方法

从 PHP manual for count():

Returns the number of elements in var. If var is not an array or an object with implemented Countable interface,1 will be returned.

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读