PHP
  Home arrow PHP arrow Page 4 - Building XML Trees With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building XML Trees With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2003-02-20

    Table of Contents:
  • Building XML Trees With PHP
  • A Hero Is Born
  • Anatomy Class
  • A La Carte
  • Slice And Dice
  • Killing Off The Kids
  • Rank And File
  • Spider, Spider On The Wall...
  • Making Friends And Influencing People
  • Doing The Chameleon
  • Linking Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building XML Trees With PHP - A La Carte


    (Page 4 of 11 )

    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


     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT