Home arrow XML arrow Page 5 - SimpleXML

Putting it All to Use - XML

Learn an easy way to parse XML and output it the way you want by using the simpleXML extensions in PHP5. Murray outlines the 3 main elements of an XML document and how to replace them with your own non-template  data and how to build an array of all the tags put in the document.

TABLE OF CONTENTS:
  1. SimpleXML
  2. Replace Tags with Our Data
  3. Build List of Tags
  4. Load the XML File and Output It
  5. Putting it All to Use
By: James Murray
Rating: starstarstarstarstar / 44
June 22, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: