XML
  Home arrow XML arrow Page 5 - SimpleXML
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? 
XML

SimpleXML
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 44
    2004-06-22


    Table of Contents:
  • SimpleXML
  • Replace Tags with Our Data
  • Build List of Tags
  • Load the XML File and Output It
  • Putting it All to Use

  • 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


    SimpleXML - Putting it All to Use
    ( Page 5 of 5 )

    That’s a wrap, err, a parse and an echo, then a wrap:

    Now we need to use all this for something worthwhile. First we’ll need to set up our elements array for the parser. for this we’ll use the add_elem() function we just made in our class.

    $template = new template();
    $template->add_elem("header","header");
    $template->add_elem("body","body");
    $template->add_elem("footer","footer");

    as you can see all you do is call the function an pass the names of each element to it, that will build an array in out class called $elements. Personally I just use the name of the element as the array key; it’s just less confusing for me if I need to debug anything in my site. Now we need to build the array of all the tags that we put in our XML document. We’ll use our add_tags() function for that. It works like the add_elem() does, only with a slight difference.

    $template->add_tags("title","This is the simpleXML test page");
    $template->add_tags("logo","simpleXML is simple!");
    $template->add_tags("column1","This is column one");
    $template->add_tags("column2","This is column two");
    $template->add_tags("column3","This is column three");
    $template->add_tags("footer","this is the footer information");

    As you can see here, you name the key the same thing that the tag is going to be named, while the value of the key is the information you want to the tag to be replaced by. The value can contain anything it can be a function that has been loaded into a variable ( $var=function()), it can be a number, text, html, more php, anything, as long as it can be outputted to a browser.

    After we’ve built our arrays of elements and tags, we’ll have to set our file path and filename that’s pretty easy and painless as well.

    $template->xml_path = "path/to/xmlfile/";
    $template->xml_file="template.xml";

    If the XML file is in the same directory as the index page, then you’ll still need to set the xml_path due to the error checking,  but you can set is to a blank.

    $template->xml_path="";

    That’s almost all that’s left to do now all we have to do is call our function to load the XML file and the function that will output it all to the browser and that’s about it!

    $template->load();
    echo $template->parse_elem();

    That’s it, pretty painless. As you can see the entire contents of all 3 files is less than the contents of most PHP4 version XML parsers, and this has a lot more functionality. This script can be expanded to do a lot more than it does here, in fact I encourage you to expand the script to do amazing things. I know that this script can be expanded to do a lot more than it does as it’s presented in this article because I use the exact same script expanded to do many more things. For example I set up the 3 columns as separate XML elements so that I can add flexibility to my layouts. I haven’t covered all the things you can do with simpleXML in this article, so I also encourage you to check out the rest of the functions that simpleXML offers and play around with them. Who knows what cool things you can actually do with it, because as every PHP developer knows, you can do anything you can imagine with PHP.



     
     
    >>> More XML Articles          >>> More By James Murray
     

       

    XML ARTICLES

    - Flex Array Collection Sort and Filtering
    - The Flex Tree Control
    - Flex List Controls
    - Working with Flex and Datagrids
    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...





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