Почему count (DOMNode-> childNodes) возвращает неверное количество детей? (PHP и XML)

У меня есть функция PHP, называемая processDeleteForm (), предназначенная для удаления указанного узла из XML-документа с именем structure.xml. Единственная проблема, с которой я сталкиваюсь до сих пор, заключается в том, что в processDeleteForm () он ищет удаляемый узел, перейдя через каждый дочерний узел родительского узла и сопоставляя его с атрибутом «name», но я убежден 't удалось получить правильное количество дочерних узлов для цикла, поэтому оно останавливается, пока не достигнет правильного узла. Соответствующий код:

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,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-файла, но я не могу понять, как его отображать как таковой. Если кто-то, у кого больше знаний об форматировании xml на stackoverflow, может это исправить, я был бы очень обязан.

Также может быть полезно увидеть код, ответственный за создание каталогов с именем «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", "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:\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 для count () :

Возвращает количество элементов в var. Если var не является массивом или объектом с реализованным интерфейсом Countable, возвращается 1.

DOMNode::$childNodes – объект DOMNodeList . Это не Countable . Он всегда будет возвращать один (даже если пустой). Вместо этого используйте свойство length :

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

Я предполагаю, что ваш childNodes включает текстовые узлы вместе с узлами XML-тегов из-за пробелов между тегами. Это может привести к тому, что число childNodes два раза больше, чем вы ожидаете. Вы должны проверить nodeType чтобы убедиться, что это не текстовый узел, прежде чем пытаться получить его атрибуты.

Возможно, здесь:

  for($x = 0; $x < count($node->childNodes); $x++) { // Verify that you're not working on a text node (type 3 TEXT_NODE) if($node->nodeType != 3 && $node->childNodes->item($x)->getAttribute("name") == $filename) { $target = $node->childNodes->item($x); echo "Target found.<br/>"; } else { echo "Searching for target...<br/>"; } }