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

Killing Off The Kids - 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
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
 

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: