XML
  Home arrow XML arrow Page 5 - SimpleXML
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    XML ARTICLES

    - 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...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway