-
December 16th, 2009, 02:53 PM
#1
PHP - writing a directory into an XML file
I am having trouble writing my file directory of my website in to an XML file. I've searched all over google and have had no success with the examples that I've found. I've checked the php documentation but can't find anything really.
here's the code i've been working with:
PHP Code:
$contents = "<?xml version=\"1.0\" ?>\n"; $path = "easythanks/";
function outputPath($path){ $level=substr_count($path,'/'); for ($i=1;$i<$level;$i++) echo ' '; echo basename($path); echo "\n"; }
echo '<pre>'; traverseDirTree('./','outputpath','outputpath'); echo '</pre>';
function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){ $subdirectories=opendir($base); while (($subdirectory=readdir($subdirectories))!==false){ $path=$base.$subdirectory; if (is_file($path)){ $contents .= "<item text=".$path." />"; if ($fileFunc!==null){ $fileFunc($path); } }else{ $contents .= "<item text=".$path.">"; if ($dirFunc!==null) $dirFunc($path); if (($subdirectory!='.') && ($subdirectory!='..')){ traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc); } if ($afterDirFunc!==null) $afterDirFunc($path); $contents .= "</item>"; } } }
$myFile = "test.xml"; $fh = fopen($myFile, 'a') or die("can't open file"); fwrite($fh, $contents); fclose($fh);
Can anyone please point me in the right direction? PS the xml file needs to look like this
Code:
<item text="directory1">
<item text="directory2">
<item text="child1" />
</item>
<item text="child2" />
<item text="child3"/>
</item>
Thanks,
David
Last edited by PeejAvery; December 17th, 2009 at 10:30 AM.
Reason: Added code tags.
-
December 16th, 2009, 08:48 PM
#2
Re: PHP - writing a directory into an XML file
I would normally do it via Dom
where is you root tag?
HTML Code:
<?xml version="1.0" ?>
<items>
<item text="directory1">
<item text="directory2">
<item text="child1" />
</item>
<item text="child2" />
<item text="child3"/>
</item>
</items>
PHP Code:
$filename = "test.xml"; $doc = new DOMDocument('1.0'); $doc->preserveWhiteSpace = false; $doc->Load($filename); $doc->formatOutput = true;
$items = $doc->createElement("items"); $doc->appendChild($items);
$path = "easythanks/";
function outputPath($path){ $level=substr_count($path,'/'); for ($i=1;$i<$level;$i++) echo ' '; echo basename($path); echo "\n"; }
echo '<pre>'; traverseDirTree($items,'./','outputpath','outputpath'); echo '</pre>';
function traverseDirTree($cNode, $base,$fileFunc,$dirFunc=null,$afterDirFunc=null){ $subdirectories=opendir($base); while (($subdirectory=readdir($subdirectories))!==false){ $path=$base.$subdirectory; if (is_file($path)){ $item = $doc->createElement("item"); $item->set_attribute('text', $path); $cNode->appendChild($item);
if ($fileFunc!==null){ $fileFunc($path); } }else{ $item = $doc->createElement("item"); $item->set_attribute('text', $path); $cNode->appendChild($item); if ($dirFunc!==null) $dirFunc($path); if (($subdirectory!='.') && ($subdirectory!='..')){
traverseDirTree($cNode,$path.'/',$fileFunc,$dirFunc,$afterDirFunc);
} if ($afterDirFunc!==null) $afterDirFunc($path); } } }
$doc->save($filename);
Note untested code
Last edited by Roguebfl; December 17th, 2009 at 12:40 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|