Location: PHPKode > scripts > XML Weblication > xml-weblication/lib/xml.php
<?

/**
  * Application XML for weblication
  * @author Andreas Altendorfer <hide@address.com>
  * @date 2003-12-06
  */
  
  
$depth = array();

$xml_item = false;

function startElement($parser, $name, $attrs) {
    
	global $depth,$xml_item;
    
	
	if ( false == $xml_item ) 
	{
	  $xml_item = new xmlitem( $parser, false, $name, $attrs );
	}
	else {
	  $me = $xml_item;
	  $xml_item = new xmlitem( $parser, $me, $name, $attrs );
	}
	
    $depth[$parser]++;
}


function endElement($parser, $name) {
    global $depth,$xml_item;
	
    $p = $xml_item->prevItem;
	if ( $p ) {
	  $xml_item->endElement();
	  $p->addChild( $xml_item );
	  $xml_item = $p;
    }
	else  {
	  $xml_item->endElement();
	}
	$depth[$parser]--;
	
	/*echo "\n-----D-e-b-u-g-------\n".
	     " Element 1 = " . $xml_item_list[0]->name . "\n" .
		 " count Children = " . count( $xml_item_list[0]->children ) . "\n"; 
	*/
}   

function characterData($parser, $data) 
{
    global $xml_item;
	if ( trim($data) > "" ) {
	  //print "Add Data <em>".trim($data)."</em> to $xml_item->name \n";
	  $xml_item->data .= trim(substr($data,0)). "\n";
    }
} 
  
  
class xml
{

   var      $filename;
   var      $counter;
   var      $parser;
   var      $element;
   
   //////////////////////////////////////////////////////////////////////
   //  INTERFACE
   //////////////////////////////////////////////////////////////////////
   // Constructor
   function xml( $ifile ) {
      $this->filename = $ifile; 
   }
   
   function free() {
     xml_parser_free($this->parser); 
   }

   function load() {
     global $xml_item_list, $xml_item;
	 
	 $xml_item_list = array();
	 $xml_item = false;
	 
	 if ( !file_exists( $this->filename )) return( false );
   
     $this->parser = ($xml_parser = xml_parser_create());
	// use case-folding so we are sure to find the tag in $map_array
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	if (!($fp = fopen($this->filename, "r"))) {
		Fatal("could not open XML input " . $this->filename );
	}
	
	while ($data = fread($fp, 4096)) {
	  if (!xml_parse($xml_parser, $data, feof($fp))) {
		Fatal(sprintf("XML error: %s at line %d",
						xml_error_string(xml_get_error_code($xml_parser)),
						xml_get_current_line_number($xml_parser)));
	  }
    }
	/*print  
	  "----------------------------------------\n" .
	  $xml_item->display() .
	  "----------------------------------------\n";
    */
	$this->element = $xml_item;
	return( $this );
	
   }
   
   
   function execute() {
     $rc = "<pre>Execute XML-File: " . $this->filename . "</pre>";
	 $rc .= $this->element->display();
	 return( $rc );
   }
   
  
   
   //////////////////////////////////////////////////////////////////////
   //  PRIVATE
   //////////////////////////////////////////////////////////////////////
  
  
} //- class xml

?>
Return current item: XML Weblication