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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: