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

Rank And File - 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
In addition to dynamically creating an XML document tree, XMLTree also allows you to read an existing tree into memory, via its getTreeFromFile() method. Consider the following XML file, named "me.xml":


<?xml version="1.0"?> <me> <name>John Doe</name> <age>16</age> <sex>male</sex> </me>
Let's now read this into an XML document tree with the getTreeFromFile() method:

<?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree("me.xml"); // read file contents $root =& $tree->getTreeFromFile(); // print tree $tree->dump(); ?>
In this case, the getTreeFromFile() method has been used to read the contents of the XML file specified in the object constructor, and convert it into an XMLTree object. The tree can then be viewed via a call to dump().

Take a quick peek at the object created by getTreeFromFile() with the print_r() function, and here's what you'll see:

xml_tree_node Object ( [attributes] => Array ( ) [children] => Array ( [0] => xml_tree_node Object ( [attributes] => Array ( ) [children] => Array ( ) [content] => John Doe [name] => name ) [1] => xml_tree_node Object ( [attributes] => Array ( ) [children] => Array ( ) [content] => 16 [name] => age ) [2] => xml_tree_node Object ( [attributes] => Array ( ) [children] => Array ( ) [content] => male [name] => sex ) ) [content] => [name] => me )
As you can see, the XML document is converted into a series of nested arrays, each one representing a node on the document tree.

Similar, though not identical, is the getTreeFromString() method, which converts an XML-compliant string into an XMLTree object. Take a look:

<?php // include class include("XML/Tree.php"); // instantiate object $tree = new XML_Tree(); // create string $str = "<?xml version='1.0'?> <me> <name>John Doe</name> <age>16</age> <sex>male</sex> </me>"; // read string into tree $root =& $tree->getTreeFromString($str); // print tree $tree->dump(); ?>


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

Dev Shed Tutorial Topics: