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

Spider, Spider On The Wall... - 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
XMLTree doesn't just restrict you to XML elements - you can easily add attributes to any element as well, simply by specifying them as an associative array of name-value pairs in your calls to the addRoot() or addChild() methods. Here's an example,


<?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree(); // add the root element $root =& $tree->addRoot("superhero", NULL, array("owner" => "Marvel Comics")); // 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(); ?>
and here's the output:

<?xml version="1.0"?> <superhero owner="Marvel Comics"> <name>Peter Parker aka Spiderman</name> <age>21</age> <sex>male</sex> <location>Manhattan</location> </superhero>
This works with the addChild() method as well - here's an example,

<?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree(); // add the root element $root =& $tree->addRoot("superheroes"); // add child elements $name =& $root->addChild("member", "Spiderman", array("sex" => "male", "location" => "Manhattan")); $name =& $root->addChild("member", "Superwoman", array("sex" => "female", "location" => "Metropolis")); $name =& $root->addChild("member", "Batman", array("sex" => "male", "location" => "Gotham City")); // print tree $tree->dump(); ?>
and here's the XML output.

<?xml version="1.0"?> <superheroes> <member sex="male" location="Manhattan">Spiderman</member> <member sex="female" location="Metropolis">Superwoman</member> <member sex="male" location="Gotham City">Batman</member> </superheroes>


 
 
>>> 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 2 - Follow our Sitemap

Dev Shed Tutorial Topics: