XML
  Home arrow XML arrow Page 4 - SimpleXML
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
XML

SimpleXML
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    SimpleXML - Load the XML File and Output It
    ( Page 4 of 5 )

    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
     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek