为什么 count(DOMNode->childNodes) 返回不正确的子节点数?(PHP 和 XML)
我有一个名为 processDeleteForm() 的 PHP 函数,旨在从名为 structure.xml 的 xml 文档中删除指定的节点.到目前为止我遇到的唯一问题是在 processDeleteForm() 中,它通过循环遍历父节点的每个子节点来搜索要删除的节点,并通过name"属性匹配它,但我没有'无法为循环获得正确数量的子节点,因此它在到达正确节点之前停止.相关代码:
I have a PHP function called processDeleteForm() designed to remove a specified node from an xml document named structure.xml. The only problem I'm having with it so far is that in processDeleteForm(), it searches for the node to be removed by looping through each child node of the parent node, and matches it by the "name" attribute, but I haven't been able to get the correct number of child nodes for the loop, so it stops before it reaches the correct node. Relevant code:
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());
}
结构.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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<directory name="music" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<directory name="She" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<directory name="Chiptek" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<directory name="mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<file name="intro.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">intro.mp3</file>
<file name="music.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">music.mp3</file>
<file name="supersonic.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">supersonic.mp3</file>
<file name="memories.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">memories.mp3</file>
<file name="chiptek.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">chiptek.mp3</file>
<file name="intermission.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">intermission.mp3</file>
<file name="kicks.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">kicks.mp3</file>
<file name="1997.mp3" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/mpeg">1997.mp3</file>
</directory>
<directory name="ogg" owner="elarsen" read="aclum,ahoffman,apavlowski,bvollmer,dpotts,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" write="aclum,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="directory">
<file name="intro.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">intro.ogg</file>
<file name="music.ogg" owner="elarsen" read="aclum,ahoffman,apavlowski,bvollmer,dpotts,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" write="aclum,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">music.ogg</file>
<file name="supersonic.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">supersonic.ogg</file>
<file name="memories.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">memories.ogg</file>
<file name="chiptek.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">chiptek.ogg</file>
<file name="intermission.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">intermission.ogg</file>
<file name="kicks.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,tperkins" type="audio/ogg">kicks.ogg</file>
<file name="1997.ogg" 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,ahoffman,apavlowski,bvollmer,dpotts,drichardson,epiatt,jcantrell,jreeve,kdouglas,kjab,lbrewer,lholliday,mfriedman,norty,pmoore,rlongwell,scatlett,sketcherside,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 的知识的人可以修复它,我将非常感激.
*The above is supposed to be the contents of an xml file, but I can't figure out how to display it as such. If somebody with more knowledge about formatting xml on stackoverflow could fix it, I would be much obliged.
查看负责创建名为test01"、test02"和test03"的目录的代码也可能很有用:
It might also be useful to see the code responsible for the creation of the directories named "test01", "test02", and "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", "w");
fwrite($file, $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 手册 for count():
返回 var 中的元素数.如果 var 不是数组或实现了 Countable 接口的对象,则返回 1.
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::$childNodes
是一个 DOMNodeList
对象.它不是 Countable
.它总是会返回一个(即使是空的).使用它的 length
改为财产:
$count = $element->childNodes->length;
更新: DOMNodeList
从 PHP 7.2 开始Countable
.
Update: DOMNodeList
has been made Countable
from PHP 7.2 onwards.
相关文章