PHP Classes
Icontem

File: XmlParser.class.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Eren Ezgü  >  XML Parser (XML to array)  >  XmlParser.class.php  
File: XmlParser.class.php
Role: Class source
Content type: text/plain
Description: XmlParser Class file
Class: XML Parser (XML to array)
Build an array with a XML document structure
 

Contents

Class file image Download
<?
/**
* @desc XML Parser Class (returns xml data as array)
* @author Eren Ezgü [eezgu at eezgu.com]
*
* @example
*       $parser = new XmlParser($xml_input);
*       $result = $parser->parse();
* @example
*       $parser = new XmlParser();
*       $result = $parser->parse($xml_input);
*/
class XmlParser{
    var 
$xml_parser;
    var 
$xml_input=null;

    var 
$elements = array();
    var 
$index_arr = array();
    var 
$ref;

    
/**
    * @desc Constructor
    * @param string
    */
    
function XmlParser($xml=null){
        
$this->xml_input $xml;
    }
    
/**
    * @desc Parsing function
    * @param string
    */
    
function parse($xml=null){
        
$this->xml_input $xml;
        if(
$this->xml_input==null){
            return 
false;
        }
        
$this->xml_parser xml_parser_create();
        
xml_set_object($this->xml_parser$this);
        
xml_set_element_handler($this->xml_parser,"startElement","endElement");
        
xml_set_character_data_handler($this->xml_parser,"characterData");
        if(!
xml_parse($this->xml_parser$this->xml_input)){
            return 
false;
        }
        
xml_parser_free($this->xml_parser);
        return 
$this->elements;
    }

    function 
startElement($parser,$tagName,$attrs){
        
$this->ref=&$this->elements;
        foreach(
$this->index_arr as $index){
            
$this->ref = &$this->ref[$index];
        }
        
$this->ref[] = array('tag'=>$tagName,'attr'=>$attrs,'data'=>'','children'=>array());
        
$i end(array_keys($this->ref));
        
array_push($this->index_arr,$i);
        
array_push($this->index_arr,'children');
    }

    function 
characterData($parser$data){
        
$index_arr $this->index_arr;
        
array_pop($index_arr);
        
$ref=&$this->elements;
        foreach(
$index_arr as $index){
            
$ref = &$ref[$index];
        }
        
$ref['data']=$data;
    }

    function 
endElement($parser,$tagName){
        
array_pop($this->index_arr);
        
array_pop($this->index_arr);
    }
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products