kellan4459
IS-IT--Management
I am using the create_xml_parser function along with the starttaghandler and endtaghandler functions. I have a class called entry that stores values for each entry created. I want to parse my xml file and in starttaghandler I set the current tag name and if it is equal to entry I want to create a new instance of entry. Then in the cDataHandler method I want to check the current tag and set the appropriate variable based on the tag. Then in the endtaghandler I want to add that instance to the entrylist array that will keep up with all of my entries in the xml file. Below is my code, my variables do not keep their values. I'm a newbie so I'm sure I'm missing a concept somewhere. Can someone give me a heads up on what I'm missing?
<?PHP
include "class.entry.php";
$currentEntry=new entry();
$currentTag="";
$counter=0;
$mylist=array();
$parser = xml_parser_create();
// register the tag and data handling functions
// with the parser
xml_set_element_handler($parser,
"startTagHandler",
"endTagHandler");
xml_set_character_data_handler($parser, "cdataHandler");
// Open the file, here hardcoded,
//that holds the XML
if (!($xmlfile = fopen("../xmlDocs/news.xml", "r")))
{
die("Could not open the file for reading");
}
// Read data from the file in 2K blocks and send it
// off to the parser. Any error will trigger a call
// to die reporting the line and column number that
// the error occured within the source XML file
// !!!NOT!!! the PHP script
while ($xmldata = fread($xmlfile, 2048))
{
if (!xml_parse($parser, $xmldata, feof($xmlfile)))
{
die(sprintf("XML error at line %d, column %d",
xml_get_current_line_number($parser),
xml_get_current_column_number($parser)));
}
}
// set up the function that will handle any opening
// tags. It must accept the tagname and any
// attributes
function startTagHandler($parser,
$tagname,
$attributes)
{
if($tagname=="ENTRY")
{
$this->currentEntry = new entry();
$this->currentTag=$tagname;
}
}
// set up the function for any end tags
// end tags dont have attributes so we can simply
// accept the tagname for closure
function endTagHandler($parser,
$tagname)
{
if($tagname=="ENTRY")
{
$this->mylist[$this->counter] = $this->currentEntry;
//call display function in entry class
$this->mylist[$this->counter]->display_entry();
$this->counter++;
}
}
// set up the function for any character data
function cdataHandler($parser,
$data)
{
if (!ereg("^[ \f\r\t\n]+$",$data))
{
if($this->currentTag=="AUTHOR")
{
$this->currentEntry->Author=$data;
}
if($this->currentTag=="DATE")
{
$this->currentEntry->Date=$data;
}
if($this->currentTag=="TIME")
{
$this->currentEntry->Time=$data;
}
if($this->currentTag=="TITLE")
{
$this->currentEntry->Title=$data;
}
if($this->currentTag=="BODY")
{
$this->currentEntry->Body=$data;
}
}
}
?>
<?PHP
include "class.entry.php";
$currentEntry=new entry();
$currentTag="";
$counter=0;
$mylist=array();
$parser = xml_parser_create();
// register the tag and data handling functions
// with the parser
xml_set_element_handler($parser,
"startTagHandler",
"endTagHandler");
xml_set_character_data_handler($parser, "cdataHandler");
// Open the file, here hardcoded,
//that holds the XML
if (!($xmlfile = fopen("../xmlDocs/news.xml", "r")))
{
die("Could not open the file for reading");
}
// Read data from the file in 2K blocks and send it
// off to the parser. Any error will trigger a call
// to die reporting the line and column number that
// the error occured within the source XML file
// !!!NOT!!! the PHP script
while ($xmldata = fread($xmlfile, 2048))
{
if (!xml_parse($parser, $xmldata, feof($xmlfile)))
{
die(sprintf("XML error at line %d, column %d",
xml_get_current_line_number($parser),
xml_get_current_column_number($parser)));
}
}
// set up the function that will handle any opening
// tags. It must accept the tagname and any
// attributes
function startTagHandler($parser,
$tagname,
$attributes)
{
if($tagname=="ENTRY")
{
$this->currentEntry = new entry();
$this->currentTag=$tagname;
}
}
// set up the function for any end tags
// end tags dont have attributes so we can simply
// accept the tagname for closure
function endTagHandler($parser,
$tagname)
{
if($tagname=="ENTRY")
{
$this->mylist[$this->counter] = $this->currentEntry;
//call display function in entry class
$this->mylist[$this->counter]->display_entry();
$this->counter++;
}
}
// set up the function for any character data
function cdataHandler($parser,
$data)
{
if (!ereg("^[ \f\r\t\n]+$",$data))
{
if($this->currentTag=="AUTHOR")
{
$this->currentEntry->Author=$data;
}
if($this->currentTag=="DATE")
{
$this->currentEntry->Date=$data;
}
if($this->currentTag=="TIME")
{
$this->currentEntry->Time=$data;
}
if($this->currentTag=="TITLE")
{
$this->currentEntry->Title=$data;
}
if($this->currentTag=="BODY")
{
$this->currentEntry->Body=$data;
}
}
}
?>