Hello,
I am a little stumped here! I wrote a class to parse some XML and populate a news article object. Here's what's happening:
-- When it encounters a <newsarticle> node, it creates a new NewsStory object, which is scoped to the entire parser class.
-- For every subsequent node, it populates the news article from the XML into the appropriate members (author, story, etc). I've verified that this is working, but only at the specific instance of the function.
-- when it encounters a </newsarticle> tag, it calls the insert() function for the news story to get put into the database.
THE PROBLEM: by this time, when it inserts, none of the data that has been populated into the object is remaining.
This seems like a simple coding issue, I've done this sort of thing before, but not with XML. Is there something I need to do to retain the scope of the news story?? Any ideas?
PLEASE HELP, THANKS A LOT!
- Frizzo
CODE:
The XML:
<mergemedia_news>
<newsstory>
<title><![CDATA[79 Percent Of Americans Missing The Point Entirely]]></title>
<author><![CDATA[Joe Smo]]></author>
<author_desc><![CDATA[Blaha]]></author_desc>
<headline><![CDATA[Headline here ]]></headline>
<sub_headline><![CDATA[subheadline here ]]></sub_headline>
<teaser_txt><![CDATA[Teaser Text here]]></teaser_txt>
<story_txt><![CDATA[ Story text here]]></story_txt>
<ext_txt><![CDATA[ extended text here]]></ext_txt>
</newsstory>
</mergemedia_news>
The XML Parser Class!
class news_xml_parser{
var $xml_parser;
var $xml_current; // tree-path to the current element
var $element; // the current element name
var $element_data;
var $story; // the story object
function news_xml_parser(){
$this->xml_parser = xml_parser_create("UTF-8");
xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->xml_parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->xml_parser, "cdata");
}
function xml2db($xml){
global $ret_msg;
xml_set_object($this->xml_parser, &$this);
if (!xml_parse($this->xml_parser, $xml)) {
$ret_msg->add_err_msg("Could not parse XML!");
} else {
$ret_msg->add_ret_msg("Parsed OK");
}
xml_parser_free($this->xml_parser);
}
function tag_open($parser, $tag, $attributes){
global $ret_msg;
$this->element = $tag;
$this->xml_current.="/~".$tag;
switch ($tag){
case "newsstory":
// instantiate new story to $this->story
$this->story = new NewsStory();
break;
default:
break;
}
}
function tag_close($parser, $tag){
global $ret_msg;
$xml_caret_pos=strrpos($this->xml_current,"/~");
$this->xml_current=substr($this->xml_current,0,$xml_caret_pos);
switch ($tag){
case "newsstory":
// insert story into the database
$this->story->insert();
break;
}
}
function cdata($parser, $cdata){
global $ret_msg;
$x = $this->element;
$s = $this->story;
switch ($x){
case "title":
$s->set_title($cdata);
break;
case "headline":
$s->set_headline($cdata);
break;
case "sub_headline":
$s->set_sub_headline($cdata);
break;
case "author":
$s->set_author($cdata);
break;
case "author_desc":
$s->set_author_desc($cdata);
break;
case "teaser_txt":
$s->set_teaser_txt($cdata);
break;
case "story_txt":
$s->set_story_txt($cdata);
break;
case "ext_txt":
$s->set_ext_txt($cdata);
break;
}
}
}