Directory Hierarchy
Posted: Thu Jan 14, 2010 5:52 am
I'm needing to write a php script that'll read a flat directories files, and then produce a hierarchical XML view from it.
Each level of the Hierachy is seperated with a ' - ' in the file name, so for example if i had the files
Test - Example - Whatever.xml
Test - Example - Whatever2.xml
Test - Whatever.xml
i'd effectively need an output of something like this;
Although in reality there will be 100's of files.
I've tried jumping straight into producing XML from the start, and have decided after a few hours that's somewhat of a optimistic view >_>, so i've gone back to basics and have decided to deal with all actual filenames until the very end, as it should be fairly simple to locate where they go, and slot them in. So i'm currently working on currently getting the structure.
Currently this is what i'm working with;
currently in the test directory i have some simple test files;
Test - Test.xml
Test.xml
Test - Test2.xml
Test - Example - Test.xml
Copy - Example - Test.xml
Test - Example - Part - Test.xml
Copy - Test - Test.xml
and the script is outputting
for example, i'd want the output to become
I've had a look around at other directory -> XML scripts, but all of the ones i've found act on using folders to distinguish between different levels of the hierarchy, rather than characters within the filename.
Cheers for any help offered.
Each level of the Hierachy is seperated with a ' - ' in the file name, so for example if i had the files
Test - Example - Whatever.xml
Test - Example - Whatever2.xml
Test - Whatever.xml
i'd effectively need an output of something like this;
Code: Select all
<structure>
<layer name="Test">
<layer name ="Example>
<layer name ="Whatever.xml"/>
<layer name ="Whatever2.xml"/>
</layer>
<layer name ="Whatever.xml"/>
</layer>
</structure>I've tried jumping straight into producing XML from the start, and have decided after a few hours that's somewhat of a optimistic view >_>, so i've gone back to basics and have decided to deal with all actual filenames until the very end, as it should be fairly simple to locate where they go, and slot them in. So i'm currently working on currently getting the structure.
Currently this is what i'm working with;
Code: Select all
<?php
$directory = ".//test";
$toWrite = "";
$handler = opendir($directory);
$started = false;
$nodes[] = "";
$count = 0;
$count2 = 0;
while ($file = readdir($handler))
{
if ($file != '.' && $file != '..')
{
list($file1, $file2) = split('[.]', $file);
if ($file2 == "xml")
{
$count = 0;
$intoArray = "";
$pieces = explode(" - ", $file);
array_pop($pieces);
if (count($pieces) == 0) //ignore single files for now
{
}
else
{
while($pieces[$count])
{
$intoArray .= $pieces[$count].$count."#";
$count++;
}
if ($started) //if array has a value in it, search
{
if (!(in_array($intoArray, $nodes2)))
{
$nodes2[$count2] = $intoArray;
$count2++;
}
}
else //skips search phase, only occurs at start
{
$nodes2[$count2] = $intoArray;
$count2++;
$started = true;
}
}
}
}
}
print_r($nodes2);
?>
Test - Test.xml
Test.xml
Test - Test2.xml
Test - Example - Test.xml
Copy - Example - Test.xml
Test - Example - Part - Test.xml
Copy - Test - Test.xml
and the script is outputting
The exact match search i have running is working fine, but i've now come to the point where i want to concatenate similar nodes.Array
(
[0] => Copy0#Example1#
[1] => Copy0#Test1#
[2] => Test0#Example1#Part2#
[3] => Test0#Example1#
[4] => Test0#
)
for example, i'd want the output to become
Any thoughts/ideas/suggestions?Array
(
[0] => Copy0#Example1#Test1#
[1] => Test0#Example1#Part2#
)
I've had a look around at other directory -> XML scripts, but all of the ones i've found act on using folders to distinguish between different levels of the hierarchy, rather than characters within the filename.
Cheers for any help offered.