#native_company# #native_desc#
#native_cta#

xmlTemplate

By Brian Sanders
on December 13, 2002

Version: 0.1.0

Type: Class

Category: Other

License: GNU General Public License

Description: This class is used to load an XML document into an associative array, using xpat. Each element is given a unique id within its element group, and each element contains the name and id of its parent, thereby retaining the hierarchy of the source xml document.

<?

class XMLTemplate{

/*

  name: XMLTemplate
  version: 0.1.0
  release: 2002/12/13
  license: GNU General Public License
  project: open
  author: Brian Sanders
  company: The Hotchkiss School
  email: [email protected]
  URL: http://www.hotchkiss.org
  description:
    This class is used to load an XML document into an associative array.
    Each element is given a unique id, and each element contains the id of 
    its parent, thereby retaining the hierarchy of the source xml document.
    The format of the array $xml_file is:

      document
        |-parent ID
          |-element ID
            |-element name
            |-attributes
            |-data

  changes:
    2002/12/13 - changed organization of array $xml_file for easier reference 
  requires:
    xpat

*/

/*

  API

    The following methods can be called for an object of this class:
      parse( $xml_file )
        definition: loads XML file $xml_file into associative array $xml_data
        parameters:
          $xml_file: full path to valid XML document or snippet
        returns: nothing
      getData()
        definition: returns the array $xml_data created by parse()
        returns: array $xml_data
      ifError()
        definition: returns 1 for object errors, 0 for no errors
        returns: 0 or 1
      getError()
        definition: returns an array of error data for the object
        returns array $error
      destruct() 
        definition: frees object resources
        returns: nothing

*/

  var $xp;
  var $xml_data;
  var $element_id;
  var $parent_ids;
  var $error;

  // Constructor
  function XMLTemplate(){
    $this->xml_data=array();
    $this->element_id=1;
    $this->parent_ids=array();
    $this->error=array();
  }

  // Destructor
  function destruct(){
    xml_parser_free( $this->xp );
  }

  // return array of xml data
  function getData(){
    return $this->xml_data;
  }

  // return 1 for an error, 0 for no error
  function isError(){
    if( sizeof( $this->error ) >0 ){
      return 1;
    }
    else{
      return 0;
    }
  }

  // return array of error messages
  function getError(){
    return $this->error;
  }

  // process xml start tag
  function startElement( $xp, $name, $atts ){
    if( sizeof( $this->parents ) <1 ){
      $temp=$this->parent_ids;
      $parent_id=array_pop( $temp );
    }
    else{
      $parent_id=0;
    }
    $this->xml_data[$parent_id][$this->element_id]['name']=$name;
    $this->xml_data[$parent_id][$this->element_id]['atts']=$atts;
    array_push( $this->parent_ids, $this->element_id );
    $this->element_id++;
  }

  // process xml end tag
  function endElement( $xp, $name ){
    array_pop( $this->parent_ids );
  }

  // process data between xml tags
  function dataHandler( $xp, $data ){
    $temp=$this->parent_ids;
    $element_id=array_pop( $temp );
    $parent_id=array_pop( $temp );
    $this->xml_data[$parent_id][$element_id]['data'].=$data;
  }

  // parse xml document $xml_file
  function parse( $xml_file ){
    if( !( $this->xp=@xml_parser_create() ) ){
      $this->error['description']='Could not create xml parser';
    }
    if( !( $this->isError() ) ){
      if( !( @xml_set_object( $this->xp, &$this ) ) ){
        $this->error['description']='Could not set xml parser for object';
      }
    }
    if( !( $this->isError() ) ){
      if( !( @xml_set_element_handler( $this->xp, 'startElement', 'endElement' ) ) ){
        $this->error['description']='Could not set xml element handler';
      }
    }
    if( !( $this->isError() ) ){
      if( !( @xml_set_character_data_handler( $this->xp, 'dataHandler' ) ) ){
        $this->error['description']='Could not set xml character handler';
      }
    } 
    if( !( $this->isError() ) ){
      if( !( $fp=@fopen( $xml_file, 'r' ) ) ){
        $this->error['description']='Could not open xml document';
      }
    }
    if( !( $this->isError() ) ){
      while( $data=fread( $fp, 4096 ) ){
        if( !( @xml_parse( $this->xp, $data, feof( $fp ) ) ) ){
          $this->error['description']=xml_error_string( xml_get_error_code( $this->xp ) );
          $this->error['line']=xml_get_current_line_number( $this->xp );
        }
      }
    }
  } 

}

?>