Location: PHPKode > scripts > xml2tree > xml2tree/fromScratch.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
 <HEAD>
  <TITLE>Test Page for xml2tree</TITLE>
 </HEAD>
  <H1 ALIGN="CENTER">
     Constructing an XML object from scratch with xml2tree...
  </H1>

  <?

  
// 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";
$newNode->printHTML();

// remove one of the first (not zero-th) inserted nodes
$newNode->deleteChild(1);

// delete one of the attributes
$newNode->deleteAttribute("name");

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

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

$newNode->deleteChild($template);
  
echo "<hr><i>Now removed child by template-matching</i> <br>\n";
$newNode->printHTML();
?>


 </BODY>
</HTML>



Return current item: xml2tree