Location: PHPKode > scripts > xml2tree > xml2tree/extracting.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
 <HEAD>
  <TITLE>Test Page for xml2tree</TITLE>
 </HEAD>
  <H1 ALIGN="CENTER">
     Extracting specific nodes using  xml2tree...
  </H1>

  <?

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



$xmlFile = "display.xml";

// read the XML file and stuff into object
$topNode = readXML ($xmlFile);

// print out the tree for reference
echo "<i>reference file:</i><br>\n";
$topNode->printHTML();
echo "<hr>\n";

// try extracting all nodes called "column" 
// Do this by creating a template node like that, then handing the
// template to extract().
$results = array();
$template = new TreeNode("column");
$topNode->extract( $results, $template);

echo "<i>Extracting all 'column' nodes....</i><br>\n";
// walk through the results array, printing out each found node and its depth
for ($idx = 0; $idx < count( $results["nodes"]); $idx++){
  $results["nodes"][$idx]->printHTML();
  echo "<i>This node at depth = " . $results["depth"][$idx] . "</i><br><br>\n";
}


// now do the same extraction, but give the template an attribute to match too
echo "<i><hr>Extracting all 'column' nodes with specific attribute 'me'....</i><br>\n";
$template->attributes["myAttr"] = "me";
$results = array();
$topNode->extract( $results, $template);

// walk through the results array, printing out each found node and its depth
for ($idx = 0; $idx < count( $results["nodes"]); $idx++){
  $results["nodes"][$idx]->printHTML();
  echo "<i>This node at depth = " . $results["depth"][$idx] . "</i><br><br>\n";
}




// use extractAtDepth to make a simpler array of nodes at known depth
echo "<i><hr>Extracting all 'foo' nodes at  depth=2....</i><br>\n";
$template = new TreeNode("foo");
$simpleResults = array();
$topNode->extractAtDepth( $simpleResults, $template, 2);

// walk through the results array, printing out each found node and its depth
for ($idx = 0; $idx < count( $simpleResults); $idx++){
  $simpleResults[$idx]->printHTML();
  echo "<br><br>\n";
}
           ?>


 </BODY>
</HTML>



Return current item: xml2tree