PHP->XML Parse Problem
| From: | Mendle | Date: | Tue, 11 May 2004 11:44:34 +0000 |
| Subject: | PHP->XML Parse Problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-185897@lists.php.net to get a copy of this message | ||
I wrote a script to parse an XML doc in straightforward PHP, but then decide
to write it as objects
but now I have a problem, that I hope someone with a little bit more
experience can look at:
Im at my wits end and would appreciate any help, im getting no errors as
such, but it is not parsing the
information.
You can read the script easier here: http://www.pastebin.com/64934
Thanks in advance,
M
========================================================
XML File (tips.xml)
========================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<newsfeed timestamp="07/04/2004 11:12" teamid="5802">
<newsstory articletype="HEADLINE1" storyid="SID1">
<headline>HEADLINE 1</headline>
<abstract>ABSTRACT TEXT - STORY 1</abstract>
<details>
<para>PARA TEXT - STORY 1</para>
</details>
</newsstory>
<newsstory articletype="HEADLINE2" storyid="SID2">
<headline>HEADLINE 2</headline>
<abstract>ABSTRACT TEXT - STORY 2</abstract>
<details>
<para>PARA TEXT - STORY 2</para>
</details>
</newsstory>
<newsstory articletype="HEADLINE3" storyid="SID3">
<headline>HEADLINE 3</headline>
<abstract>ABSTRACT TEXT - STORY 3</abstract>
<details>
<para>PARA TEXT - STORY 3</para>
</details>
</newsstory>
<newsstory articletype="HEADLINE4" storyid="SID4">
<headline>HEADLINE 4</headline>
<abstract>ABSTRACT TEXT - STORY 4</abstract>
<details>
<para>PARA TEXT - STORY 4</para>
</details>
</newsstory>
</newsfeed>
========================================================
Class File (xml.class.php)
========================================================
<?php
class XMLParser{
var $_file; // Location/Filename of XML File
var $_xparser; // Parser
var $_fp; // FilePointer
var $_data; // Data being parsed
var $_currentElement; // Current tag
var $_newstime; // Timestamp
var $_suppliers_id; // Suppliers ID
var $_article_type; // Article Type
var $_story_id; // Story ID
var $_headline; // Headline
var $_abstract; // Abstract
var $_para; // Paragragh
//Initialise XML File & parser
function XMLParser($filename){
$this->_file = $filename;
$this->_xparser = xml_parser_create();
$this->_fp = "";
$this->_data = "";
/*
$this->_currentElement = "";
$this->_newstime = "";
$this->_suppliers_id = "";
$this->_article_type = "";
$this->_story_id = "";
$this->_headline = "";
$this->_abstract = "";
$this->_para = "";
*/
}
//Set callback functions
function createCallBacks()
{
xml_set_element_handler($this->_xparser, array($this, "startElement"),
array($this, "endElement"));
xml_set_character_data_handler($this->_xparser, array($this,
"charData"));
}
//Open file for reading
function openFile()
{
if(!($this->_fp = fopen($this->_file, "r"))){
die("Cannot locate XML file: " . $this->_file);
exit();
}
}
//Start parsing
function parseFile()
{
while($this->_data = fread($this->_fp, 4096))
{
// Error Handler
if(!xml_parse($this->_xparser, $this->_data, feof($this->_fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->_xparser)),
xml_get_current_line_number($this->_xparser)));
exit();
}
}
}
//Free up parser memory
function freeParser()
{
xml_parser_free($this->_xparser);
}
//Parse START tags
function startElement(&$_xparser,$name,$attribs) {
$this->_currentElement = $name;
if ($name == "NEWSFEED") {
$this->_newstime = $attribs['TIMESTAMP'];
$this->_suppliers_id = $attribs['TEAMID'];
}
if ($name == "NEWSSTORY") {
$this->_article_type = $attribs['ARTICLETYPE'];
$this->_story_id = $attribs['STORYID'];
}
}
//Parse END tags
function endElement(&$_xparser,$name) {
$this->_currentElement = "";
if($name == "NEWSSTORY"){
// Echo would be queries to screen *DELETE AFTER DEBUG*
echo "<nobr>INSERT INTO table (timestamp, team_id, art_type, story_id,
headline, abstract, para) VALUES ('" . $this->_newstime . "','" .
$this->_suppliers_id . "','" . $this->_article_type .
"','" .
$this->_story_id . "','" . $this->_headline ."','" .
$this->_abstract .
"','" . $this->_para . "')</nobr><br>\n";
$this->_currentElement = "";
$this->_article_type = "";
$this->_story_id = "";
$this->_headline = "";
$this->_abstract = "";
$this->_para = "";
}
}
//Parse MAIN tag content
function charData(&$_xparser,$data){
switch($this->_currentElement){
case "HEADLINE":
$this->_headline = $data;
break;
case "ABSTRACT":
$this->_abstract = $data;
break;
case "PARA":
$this->_para = $data;
break;
}
}
}
?>
========================================================
Index File (index.php) (calls class)
========================================================
<?php
require_once("xml.class.php");
$xml = new XMLParser("tips.xml");
$xml->createCallBacks();
$xml->openFile();
$xml->parseFile();
$xml->freeParser();
?>