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.
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.
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.