PHP - 无法创建xml文件

时间:2014-02-20 08:43:35

标签: php xml domdocument

大家好,

下面是我最近努力使其工作的一段代码。不幸的是,它没有像我希望的那样工作。

以下代码应该查看“gallery”文件夹,查找其中的所有文件和文件夹,并将所有输出存储到xml文件中。

我希望xml文件看起来像这样..

<categories>
<movies>
file1.jpg
file2.jpg
</movies>
<games>
file1.jpg
</games>
</categories> 

依旧......

但我的下面代码却改为输出这个输出:

<?xml version="1.0"?>
<Categories/>

这是代码

<?php
    class XML_file
    {
        private $doc;
        private $root;
        private $MyXml_file_handler;
        private $node;
        private $child;

        public function __construct($root_name, $file)
        {
            $this->MyXml_file_handler = fopen($file, 'w') or die('Cannot open file:  '.$file); //implicitly creates file

            $this->doc = new DOMDocument('1.0');
            $this->node = new DOMText;
            $this->doc->formatOutput = true;
            $this->root = $this->doc->createElement($root_name);
            $this->root = $this->doc->appendChild($this->root);
            echo __CLASS__," has been created. <br />";
        }

        public function __destruct() 
        {
            fwrite($this->MyXml_file_handler, $this->doc->saveXML());
            fclose($this->MyXml_file_handler);
            echo __CLASS__," has been destroyed. <br />";
        }

        public function AppendChild($name)
        {
            $this->child = $this->doc->createElement($name);
            $this->child = $this->node->appendChild($this->child);
        }

        public function CreateNode($content)
        {
            $this->node = $this->doc->createTextNode($content);
            $this->node = $this->root->appendChild($this->node);
        }
    }

    class ImageDb
    {   
        ////// Constructor & Destructor //////////////////
        private $myXML;

        public function __construct($dir)
        {
            $this->myXML = new XML_file("Categories","myImageDB.xml");
            $this->SearchDir($dir);
            echo __CLASS__," has been created. <br />";
        }

        public function __destruct() 
        {
            echo __CLASS__," has been destroyed. <br />";
        }
        ////// Constructor & Destructor //////////////////

        function SearchDir ($dir)
        {
            if($dh  = opendir($dir))
            {
                while (false !== ($filename = readdir($dh))) 
                {$files[] = $filename;}
                rsort($files);

                foreach ($files as $key => $value) 
                {
                    if (!in_array($value,array(".",".."))) 
                    {
                        if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
                        {
                            $this->myXML->AppendChild($value);
                            echo "Directory: $value <br />";
                            $this->SearchDir("$dir$value"); 
                        }else {
                            $this->myXML->CreateNode($value);
                            echo "File: $value <br />";
                        }
                    }
                }
                closedir($dh); 
            }
        }
    }

    $image = new ImageDb("../galeria/");
?>

我无法弄清问题在哪里。

我确信它确实找到了该文件夹中的所有文件,并且还打印了所有文件 屏幕上的内容,但当我尝试将内容存储到xml文件时,它不起作用。

/////////编辑/////

我现在已经修改了XML_file类中的两个函数,并且它正在返回它。

<?xml version="1.0"?>
    <Categories>filex.txt<Space/><Nature/><Movies/>file2.txtfile1.txt<Games/></Categories>
public function AppendChild($name)
{
    $this->child = $this->doc->createElement($name);
    $this->child = $this->root->appendChild($this->child);
}

public function CreateNode($content)
{
    $this->node = $this->doc->createTextNode($content);
    $this->node = $this->root->appendChild($this->node);`
}

如您所见,它现在只打印“关闭”元素。 所以现在我只需要了解它为什么不添加“开放”元素。

非常感谢任何帮助,

提前致谢, 亚历

1 个答案:

答案 0 :(得分:0)

我已经使用 AppendChild 功能进行了游戏,结果发现他们错了。

现在输出更好了,虽然在没有分配TextNode时我仍然得到结束属性。

public function AppendChild($name)
        {
            $this->child = $this->doc->createElement($name);
            $this->child = $this->root->appendChild($this->child);
        }

        public function CreateNode($content)
        {
            $tmp = $this->doc->createElement("path");
            $this->child->appendChild($tmp);

            $textNode = $this->doc->createTextNode($content);
            $tmp->appendChild($textNode);
        }