PHP
  Home arrow PHP arrow Page 7 - 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 - Rank And File
    ( Page 7 of 11 )

    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
     

       

    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