Home arrow XML arrow Page 4 - SimpleXML

Load the XML File and Output It - 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

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.



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