<?
class Parser {
var $att;
var $id;
var $title;
var $content;
var $index=-1;
var $xml_parser;
var $tagname;
function parser()
{
$file = "data.xml";
$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, 'elementContent');
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
}
}
xml_parser_free($this->xml_parser);
}
function startElement($parser, $name, $attrs) {
if (($name=="TREE") or ($name=="NODE") or ($name=="LEAFNODE"))
{
$this->index++;
$this->att[$this->index]=$name;
}
$this->tagname=$name;
}
function elementContent($parser, $data) {
switch ($this->tagname)
{
case 'ID':
$this->id[$this->index]=trim($data);
break;
case 'TITLE' :
$this->title[$this->index]=trim($data);
break;
case 'CONTENT' :
$this->content[$this->index]=trim($data);
break;
}
}
function endElement($parser, $name){
$this->tagname=="";
}
}
?>
I thought this class is more help full to know about xml_parser with no white spaces