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.
First off you know how your own XML template file is built, so knowing what elements and tags it had won’t be a problem. Secondly it’s faster at parse time to parse a function that will loop through an array of all the elements than it is to parse multiple lines that call each of the elements alone. So what we do is make a function that will accept an array that will hold the names of all the elements, then run them through the replace() function, then output them. I actually created a class to handle the loading of the XML file, building the elements array, building the tags array, and outputting it all. Here’s what I have.
<? class template { public $xml_path; //holds the path to our template files public $xml_file; //holds the filename of the XML file public $elements; //holds the array of all our XML elements public $element; //temporary storage of an individual XML element public $load; //holds the array of all our tags public $tmpl_file; //holds the combined path and filename of our template file public $tmpf; //holds the XML file after being loaded by simpleXML
function load(){ if(isset($this->xml_path)&&isset($this->xml_file)){ //make sure that both variables are set $this->tmpl_file=$this->xml_path.$this->xml_file; //combine the XML path and filename to make one string }else{ $this->tmpl_file="fail/safe/path/to/your/xml_file.xml"; //if both variables aren’t set load a failsafe XML file } if(file_exists($this->tmpl_file)){ //make sure the set XML file exists $this->tmpf=simplexml_load_file($this->tmpl_file); //load the XML file into the variable $tmpf }else{ return "Error Loading Template Data: File Not Found"; //send an error if the XML file can’t be loaded } }
function parse_elem(){ if(is_array($this->elements)){ //make sure that the elements variable is an array foreach ($this->elements as $elem){ //loop through each element $this->element=$this->tmpf->$elem; //set current element as the value of $element if($this->element!=""){ //check to make sure the element isn’t empty $page.=$this->replace($this->load,$this->element); //add the output of the replace function to our output variable }else{ $page.="Error Loading Template Data: Element \"$elem\" Not Valid<br />"; //add an error incase the element is invalid } } }else{ return "Error Loading Template Data: Element Data Not Valid<br />";//send an error if the elements variable isn’t an array } return $page; //output the final product of our hard work }
function add_elem($key,$value){//add an element to the elements array if(isset($key)&&isset($value)){//make sure all information is specified $this->elements[$key]=$value;//add a key to the elements array containing specified data }else{ echo "Error Adding Element: Element Data Missing";//send an error incase the correct information isn’t specified } }
function add_tags($key,$value){//add a tag to the load array if(isset($key)&&isset($value)){//make sure all information is specified $this->load[$key]=$value; //add a key to the load array containing specified data }else{ //echo "Error Adding Tag: Tag Data Missing"; //send an error incase the correct information isn’t specified } }
function replace($load,$string,$mask='<!-tag->'){ if(!is_array($load)){ //make sure load variable is an array return $string;// if load variable isn’t an array return the string unprocessed }else{ if(!strpos($load,'-tag-')) $mask='<!-tag->';//make sure the tag mask isn’t in the load array $masks=explode('-tag-',$mask);//explode the mask array to reveal tag beginning and end $tags=array_keys($load);//load the keys from load array into another array called $tags foreach($tags as $tag){ //loop through tags array $newtags[]=$masks[0].$tag.$masks[1];//build array of the tags for the specified data } $loads=array_values($load);//load values from load array into a separate array named $loads return str_replace($newtags,$loads,$string);//process all tags, replace data and the specified XML string through str_replace() } } } ?>
It’s not actually all that complicated if you just take a second to look at it. Now, to put this all into use. Save the class that I just showed you as parser.php.