PHP
  Home arrow PHP arrow Page 6 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Killing Off The Kids
    ( Page 6 of 11 )

    You've already seen that you can also add elements to an existing tree via the addChild() method. But XMLTree also allows you to prune an existing document tree, deleting elements from the hierarchy with the removeChild() method.

    In order to illustrate this, consider the following example, which dynamically constructs a simple document tree:


    <?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree(); // add the root element $root =& $tree->addRoot("superhero"); // add child elements $name =& $root->addChild("name", "Peter Parker aka Spiderman"); $age =& $root->addChild("age", 21); $sex =& $root->addChild("sex", "male"); $location =& $root->addChild("location", "Manhattan"); // print tree $tree->dump(); ?>
    Here's the output:

    <?xml version="1.0"?> <superhero> <name>Peter Parker aka Spiderman</name> <age>21</age> <sex>male</sex> <location>Manhattan</location> </superhero>
    Now's let's suppose I wanted to remove the <age> element from the tree. With removeChild(), it's a piece of cake:

    <?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree(); // add the root element $root =& $tree->addRoot("superhero"); // add child elements $name =& $root->addChild("name", "Peter Parker aka Spiderman"); $age =& $root->addChild("age", 21); $sex =& $root->addChild("sex", "male"); $location =& $root->addChild("location", "Manhattan"); // remove second child node (age) $root->removeChild(1); // print tree $tree->dump(); ?>
    The removeChild() method takes, as argument, the index number of the child to be removed under the specified Node object (remember that node numbering begins at 0, not 1). In this case, since I want to remove the <age> element, which is the second child of the root element, I can use

    <?php $root->removeChild(1); ?>
    to get the job done.

    And here's the revised output:

    <?xml version="1.0"?> <superhero> <name>Peter Parker aka Spiderman</name> <sex>male</sex> <location>Manhattan</location> </superhero>


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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT