Location: PHPKode > scripts > xml2tree > xml2tree/fromScratch.php
  <TITLE>Test Page for xml2tree</TITLE>
     Constructing an XML object from scratch with xml2tree...


// include the file with all the functions and classes
require ("xml2tree.inc");

// create a node 
$newNode = new TreeNode("wibble");

// create attributes
$newNode->attributes["name"] = "glob";
$newNode->attributes["rank"] = "smelly";
$newNode->attributes["cereal"] = "cornflakes";

// insert today's date as first child
$newNode->insertChildText(date("l dS of F Y h:i:s A"), 0);

//create another node to insert
$littleNode = new TreeNode("fizz");
$littleNode->attributes["bubbles"] = "CO2";
$littleNode->attributes["copied"] = "yes";

// insert/copy the little node as a child
$newNode->insertChildCopy( $littleNode );

// insert a *reference* to the little node as another child
$newNode->insertChildNode( $littleNode );

// now *change* $littleNode  to prove the child also changes by reference
$littleNode->attributes["bubbles"] = "N2";
$littleNode->attributes["gas"] = "oops, sorry...";
$littleNode->attributes["copied"] = "no";
$littleNode->attributes["referenced"] = "yes";

// show the results: the inserted reference reflects the change, the
// inserted copy doesn't
echo "<hr><i>Constructed a node and children from scratch </i> <br>\n";

// remove one of the first (not zero-th) inserted nodes

// delete one of the attributes

// print out again after changes
echo "<hr><i>Now removed middle child and deleted 'name' attribute</i> <br>\n";

// deleting last child by describing (via template) what to kill, rather 
// than by giving position
$template = new TreeNode("fizz");
$template->attributes["bubbles"] = "N2";

echo "<hr><i>Now removed child by template-matching</i> <br>\n";


Return current item: xml2tree