Need to manipulate XML document trees, but don't have the DOM extension compiled into your PHP build? Take a look at XMLTree, a PEAR class that allows you to create and manipulate XML document trees without requiring the PHP DOM extension.
You can retrieve any segment of a dynamically-created document tree via the get() method, as in the following variant of the previous example:
<?php
// include class
include("XML/Tree.php");
// instantiate object
$tree
= new XML_Tree();
// add the root element
$root =& $tree->addRoot("recipe");
//
add children
$name =& $root->addChild("name", "Chicken Tikka");
$author =&
$root->addChild("author", "Anonymous");
$ingredients =& $root->addChild("ingredients");
$servings
=& $root->addChild("servings", 3);
$process =& $root->addChild("process");
//
create array of ingredients
$ingredientsArray = array(
"Boneless chicken breasts,
2",
"Chopped onions, 2",
"Garlic, 1 tsp",
"Ginger, 1 tsp",
"Red
chili powder, 1 tsp",
"Lime juice, 2 tbsp",
"Butter, 2 tbsp"
);
//
create
array of steps to execute recipe
$stepsArray = array(
"Cut chicken
into
cubes,
wash and apply lime juice and
salt",
"Add ginger, garlic, chili
and
lime juice
in a separate
bowl",
"Mix well, and add chicken to marinate
for
3-4 hours",
"Place
chicken pieces on skewers and barbeque",
"Remove,
apply
butter, and barbeque
again until meat is
tender",
"Garnish with lemon
and chopped
onions"
);
//
add ingredient list to tree
foreach ($ingredientsArray
as $i)
{
$item
= $ingredients->addChild("item",
$i);
}
// add execution steps
to tree
foreach
($stepsArray as $s)
{
$step =
$process->addChild("step",
$s);
}
//
print ingredients
only
echo $ingredients->get();
?>
In this case, the output of the script is the <ingredients> node only: