| 1 |
<?php |
| 2 |
// |
| 3 |
// $Id: Memory_XML.php,v 1.2 2003/01/30 17:43:41 cain Exp $ |
| 4 |
// |
| 5 |
|
| 6 |
//ini_set('include_path',realpath(dirname(__FILE__).'/../../').':'.realpath(dirname(__FILE__).'/../../../includes').':'.ini_get('include_path')); |
| 7 |
//ini_set('error_reporting',E_ALL); |
| 8 |
/** |
| 9 |
* this is a helper function, so i dont have to write so many prints :-) |
| 10 |
* @param array $para the result returned by some method, that will be dumped |
| 11 |
* @param string $string the explaining string |
| 12 |
*/ |
| 13 |
function dumpHelper( $para , $string='' ) |
| 14 |
{ |
| 15 |
global $tree; |
| 16 |
|
| 17 |
print '<i><u><font color="#008000">'.$string.'</font></u></i><br>'; |
| 18 |
// this method dumps to the screen, since print_r or var_dump dont |
| 19 |
// work too good here, because the inner array is recursive |
| 20 |
// well, it looks ugly but one can see what is meant :-) |
| 21 |
$tree->varDump($para); |
| 22 |
print '<br>'; |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* dumps the entire structure nicely |
| 28 |
* @param string $string the explaining string |
| 29 |
*/ |
| 30 |
function dumpAllNicely( $string='' ) |
| 31 |
{ |
| 32 |
global $tree; |
| 33 |
|
| 34 |
print '<i><u><font color="#008000">'.$string.'</font></u></i><br>'; |
| 35 |
$all = $tree->getNode(); // get the entire structure sorted as the tree is, so we can simply foreach through it and show it |
| 36 |
foreach( $all as $aElement ) |
| 37 |
{ |
| 38 |
for( $i=0 ; $i<$aElement['level'] ; $i++) |
| 39 |
print ' '; |
| 40 |
print '<b>'.$aElement['name'].'</b> ===> '; |
| 41 |
|
| 42 |
// you can also show all the content, using this |
| 43 |
// $tree->varDump(array($aElement)); |
| 44 |
// i just didnt, since it takes up more then the entire line, and its unreadable :-) |
| 45 |
|
| 46 |
print 'attributes - '; |
| 47 |
print_r($aElement['attributes']); |
| 48 |
print '<br>'; |
| 49 |
|
| 50 |
} |
| 51 |
print '<br>'; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/* |
| 57 |
|
| 58 |
This example demonstrates how to manage trees |
| 59 |
that are saved in an XML-file |
| 60 |
|
| 61 |
it reads out the entire file upon calling the method |
| 62 |
'setup', then you can work on the tree in whichever way |
| 63 |
you want, just have a look at the examples |
| 64 |
there are different ways to achieve things, |
| 65 |
i will try to demonstrate (all of) them |
| 66 |
|
| 67 |
NOTE: for referening the XML-Nodes currently everything has to |
| 68 |
be lower case, |
| 69 |
SimpleTemplate/preFilter |
| 70 |
should be |
| 71 |
simpletemplate/prefilter |
| 72 |
|
| 73 |
*/ |
| 74 |
|
| 75 |
require_once('Tree/Tree.php'); |
| 76 |
|
| 77 |
// calling 'setupMemory' means to retreive a class, which works on trees, |
| 78 |
// that are temporarily stored in the memory, in an array |
| 79 |
// this means the entire tree is available at all time |
| 80 |
// consider the resource usage and it's not to suggested to work |
| 81 |
// on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) |
| 82 |
// using 'setupMemory' |
| 83 |
$tree = Tree::setupMemory( 'XML', // use the XML class to read an xml file |
| 84 |
'config.xml' // the DSN |
| 85 |
); |
| 86 |
|
| 87 |
// methods 'add' 'remove' and so on are not implemented yet, you can only read the tree for now |
| 88 |
// and navigate inside of it |
| 89 |
|
| 90 |
// call 'setup', to build the inner array, so we can work on the structure using the |
| 91 |
// given methods |
| 92 |
$tree->setup(); |
| 93 |
|
| 94 |
dumpAllNicely( 'dump all after "$tree->setup"' ); |
| 95 |
|
| 96 |
// get the path of the last inserted element |
| 97 |
print 'id='.$id = $tree->getIdByPath('simpletemplate/options/delimiter'); |
| 98 |
dumpHelper( $tree->getPath( $id ) , 'dump the path from "simpletemplate/options/delimiter"' ); |
| 99 |
|
| 100 |
$id = $tree->getIdByPath('simpletemplate/options'); |
| 101 |
dumpHelper( array($tree->getParent($id)) , 'dump the parent of "simpletemplate/options"' ); |
| 102 |
// you can also use: $tree->data[$id]['parent'] |
| 103 |
|
| 104 |
$id = $tree->getIdByPath('simpletemplate'); |
| 105 |
dumpHelper( array($tree->getChild($id)) , 'dump the child of "simpletemplate"' ); |
| 106 |
// you can also use: $tree->data[$id]['child'] |
| 107 |
|
| 108 |
$id = $tree->getIdByPath('simpletemplate/prefilter'); |
| 109 |
dumpHelper( $tree->getChildren($id) , 'dump the children of "simpletemplate/prefilter"' ); |
| 110 |
// you can also use: $tree->data[$id]['children'] |
| 111 |
|
| 112 |
$id = $tree->getIdByPath('simpletemplate/options'); |
| 113 |
dumpHelper( array($tree->getNext($id)) , 'dump the "next" of "simpletemplate/options"' ); |
| 114 |
// you can also use: $tree->data[$id]['next'] |
| 115 |
|
| 116 |
$id = $tree->getIdByPath('simpletemplate/prefilter'); |
| 117 |
dumpHelper( array($tree->getPrevious($id)) , 'dump the "previous" of "simpletemplate/prefilter"' ); |
| 118 |
// you can also use: $tree->data[$id]['previous'] |
| 119 |
|
| 120 |
|
| 121 |
$id = $tree->getIdByPath('simpletemplate/preFilter'); |
| 122 |
$element = $tree->data[$id]['child']['next']['next']; // refer to the third child of 'SimpleTemplate/preFilter/register' |
| 123 |
dumpHelper( $element['id'] , 'demo of using the internal array, for referencing tree-nodes' ); |
| 124 |
|
| 125 |
/* |
| 126 |
NOT IMPLEMENTED YET |
| 127 |
|
| 128 |
$id = $tree->getIdByPath('myElement/anotherSubElement'); |
| 129 |
$tree->move( $id , 0 ); |
| 130 |
$tree->setup(); // rebuild the structure again, since we had changed it |
| 131 |
dumpAllNicely( 'dump all, after "myElement/anotherSubElement" was moved under the root' ); |
| 132 |
|
| 133 |
$moveId = $tree->getIdByPath('myElement'); |
| 134 |
$id = $tree->getIdByPath('anotherSubElement'); |
| 135 |
$tree->move( $moveId , $id ); |
| 136 |
$tree->setup(); // rebuild the structure again, since we had changed it |
| 137 |
dumpAllNicely( 'dump all, after "myElement" was moved under the "anotherSubElement"' ); |
| 138 |
|
| 139 |
|
| 140 |
$tree->setRemoveRecursively(true); |
| 141 |
$tree->remove(0); |
| 142 |
print '<font color="red">ALL ELEMENTS HAVE BEEN REMOVED (uncomment this part to keep them in the DB after running this test script)</font>'; |
| 143 |
*/ |
| 144 |
?> |