Home arrow PHP arrow Page 4 - Building XML Trees With PHP

A La Carte - PHP

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.

TABLE OF CONTENTS:
  1. Building XML Trees With PHP
  2. A Hero Is Born
  3. Anatomy Class
  4. A La Carte
  5. Slice And Dice
  6. Killing Off The Kids
  7. Rank And File
  8. Spider, Spider On The Wall...
  9. Making Friends And Influencing People
  10. Doing The Chameleon
  11. Linking Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 27
February 20, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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>


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: