Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

xml_structure_builder.php

00001 
00002 /** @file xml_structure_bulder.php
00003 * Definition of class xml_structure_builder.
00004 */
00005 #include "error_logger.php"
00006 /**
00007 * Builds tree-like structure from xml.
00008 */
00009 class xml_structure_builder : public  error_logger
00010 {
00011 public:
00012   var xml_parser = 0;
00013   var elements = array();
00014   /**
00015   * Parse data from string.
00016   * @param string data - xml data to parse.
00017   */
00018   function parse( data )
00019   {
00020     if( data == '' )
00021       return 0;
00022     else
00023     {
00024       this->elements = array();
00025       if( !this->xml_parser = xml_parser_create() )
00026         return this->err( "unable to create xml parser", "parse", __LINE__, __FILE__ );
00027       if( !xml_parser_set_option( this->xml_parser, XML_OPTION_CASE_FOLDING, 0 ) )
00028       {
00029         xml_parser_free( this->xml_parser );
00030         return this->err( "unable to disable case-folding", "parse", __LINE__, __FILE__ );
00031       }
00032       xml_set_object( this->xml_parser, this );
00033       if( !xml_set_element_handler( this->xml_parser, "tagxsb_open", "tagxsb_close" ) )
00034       {
00035         xml_parser_free( this->xml_parser );
00036         return this->err( "unable to set element handler", "parse", __LINE__, __FILE__ );
00037       }
00038       if( !xml_set_character_data_handler( this->xml_parser, "cdata_xsb" ) )
00039       {
00040         xml_parser_free( this->xml_parser );
00041         return this->err( "unable to set CDATA handler", "parse", __LINE__, __FILE__ );
00042       }
00043       if( !xml_parse( this->xml_parser, data, 1 ) )
00044       {
00045         this->err( "XML error: ".
00046           xml_error_string( xml_get_error_code( this->xml_parser ) ).
00047           " at line ".
00048           xml_get_current_line_number( this->xml_parser ), "parse", __LINE__, __FILE__ );
00049         xml_parser_free( this->xml_parser );
00050         return 0;
00051       }
00052       xml_parser_free( this->xml_parser );
00053       return this->elements;
00054     } //if( data == '' )
00055   }
00056   /**
00057   * Parse xml file.
00058   * @param string fname - name of xml file to parse
00059   */
00060   function parse_file( fname )
00061   {
00062     if( fname == '' )
00063       return this->err( "no filename specified", "parse_file", __LINE__, __FILE__ );
00064     if( !is_file( fname ) )
00065       return this->err( "file does not exist: ".fname, "parse_file", __LINE__, __FILE__ );
00066     return this->parse( join( '', file( fname ) ) );  
00067   }
00068   function tagxsb_open( parser, tag, attributes )
00069   {
00070     if( parser != this->xml_parser )
00071       return;
00072     array_push( this->elements, array( 
00073       'element_name' => tag,
00074       'attributes' => attributes ) );
00075   }
00076   function cdata_xsb( parser, cdata )
00077   {
00078     if( trim( cdata ) == '' )
00079       return;
00080     parent = & this->elements[ end( array_keys( this->elements ) ) ];
00081     parent[ 'content' ][] = array(
00082       'element_name' => 'CDATA',
00083       'attributes' => array( 'text' => cdata ) );
00084   }
00085   function tagxsb_close( parser, tag )
00086   {
00087     me = array_pop( this->elements );
00088     if( !sizeof( this->elements ) == 0 )
00089     {
00090       parent = & this->elements[ end( array_keys( this->elements ) ) ];
00091       parent[ 'content' ][] = me;
00092     }
00093     else this->elements = me;
00094   }
00095 }
00096 
00097 X-Powered-By: PHP/4.0.8-dev
00098 Content-type: text/html
00099 
00100 

Generated on Tue Mar 11 08:35:13 2003 for SES by doxygen1.2.16