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.
Here's another example, this one building a slightly more complicated document tree,
<?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 tree
$tree->dump();
?>
and here's the output:
<?xml version="1.0"?>
<recipe>
<name>Chicken Tikka</name>
<author>Anonymous</author>
<ingredients>
<item>Boneless chicken breasts, 2</item>
<item>Chopped
onions, 2</item>
<item>Garlic, 1 tsp</item>
<item>Ginger,
1 tsp</item>
<item>Red chili powder, 1 tsp</item>
<item>Lime
juice, 2 tbsp</item>
<item>Butter, 2 tbsp</item>
</ingredients>
<servings>3</servings>
<process>
<step>Cut
chicken into
cubes, wash and apply lime juice and
salt</step>
<step>Add
ginger,
garlic, chili and lime juice in a separate
bowl</step>
<step>Mix
well,
and add chicken to marinate for 3-4
hours</step>
<step>Place
chicken
pieces on skewers and barbeque</step>
<step>Remove, apply
butter,
and barbeque again until meat is
tender</step>
<step>Garnish
with
lemon and chopped onions</step>
</process>
</recipe>