使用readdir omit filetype创建xml

时间:2014-10-22 21:42:04

标签: php xml dom

我已经被困了几天,似乎无法弄清楚这一点。

我正在阅读目录并获取这些文件名并创建一个xml文件。

问题只是想要使用视频文件或图像文件。然后将xml读入带分页的表中。所以我只想要我将使用的文件,而不是.txt,.php或该目录中的其他.xml。

这是我现在拥有的代码:

  if ($handle = opendir('recordings')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {

            $loadThisUrl = "recordings/recordings.xml";

            $xmldoc = new DomDocument( '1.0' );
            $xmldoc->preserveWhiteSpace = false;
            $xmldoc->formatOutput = true;

            if( $xml = file_get_contents( $loadThisUrl ) ) {
                $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );

                // find the channels tag
                $root = $xmldoc->getElementsByTagName('channels')->item(0);

                // create the <channel> tag 
                $channel= $xmldoc->createElement('channel');




                // add ID to <channel> tag
                $id = $xmldoc->getElementsByTagName('channel')->length;
                $idAttribute = $xmldoc->createAttribute("id");
                $idAttribute->value = $id;  

                // add name to <channel> tag
                $nameAttribute = $xmldoc->createAttribute("name");
                $nameAttribute->value = $entry;

                // add url to <channel> tag
                $urlAttribute = $xmldoc->createAttribute("url");
                $urlAttribute->value = $entry;

                // add category to <channel> tag
                $categoryAttribute = $xmldoc->createAttribute("category");
                $categoryAttribute->value = "recordings";       

                // add quality to <channel> tag
                $qualityAttribute = $xmldoc->createAttribute("quality");
                $qualityAttribute->value = "best";  

                $channel->appendChild($idAttribute);
                $channel->appendChild($urlAttribute);



                $channel->appendChild($nameAttribute);
                $channel->appendChild($categoryAttribute);
                $channel->appendChild($qualityAttribute);

                // add the product tag before the first element in the <headercontent> tag
                $root->insertBefore( $channel, $root->firstChild );
                $xmldoc->save($loadThisUrl);
            }               
        }
    }
    closedir($handle);
}

0 个答案:

没有答案