Hello all,
I was designing my simple parser class but it doesn't work although I've been coding it very carefully. If someone could help, I will be very thankful. The first snipper of code is the index.php and the second one is travel.xml. The last one is the error message.
<?php
class xmlparser {
var $currentTag; // keeping track of which tag is the parser currently parsing
var $currentAttribs; // keeping track of the attributes the parser is currently parsing
var $file; // the target file
var $debug; // Set to 1 to turn on debugging or 0 to turn off debugging.
var $xmlParser;
var $caseFold;
var $targetEncoding;
var $fp;
var $data;
function startElement($parser, $name, $attribs)
{
$this->currentTag = $name;
$this->currentAttribs = $attribs;
# define the HTML to use for the start tag.
switch ($name) {
case "Recordset":
break;
case "Travelpackage":
while (list ($key, $value) = each ($attribs)) {
echo("<tr><td>$key: $value</td></tr>\n");
}
break;
case "package":
break;
default:
echo("<tr><td>$name</td><td>\n");
break;
}
}
function endElement($parser, $name)
{
# output closing HTML tags
switch ($name) {
case "Travelpackage":
echo("<tr><td colspan=\"2\"><hr></td></tr>\n");
break;
default:
echo("</td></tr>\n");
break;
}
# clear current tag variable
$this->currentTag = "";
$this->currentAttribs = "";
}
function characterData($parser, $data)
{
# add HTML tags to the values
switch ($this->currentTag) {
case "Country_name":
echo("<a href=\"#\">$data</a>\n");
break;
default:
echo($data);
break;
}
}
function init()
{
$this->xmlParser = xml_parser_create();
$this->caseFold = xml_parser_get_option($this->xmlParser,
XML_OPTION_CASE_FOLDING);
$this->targetEncoding = xml_parser_get_option($this->xmlParser,
XML_OPTION_TARGET_ENCODING);
if ($this->debug > 0) {
echo("Debug is set to: $this->debug<br>\n");
echo("Case folding is set to: $this->caseFold<br>\n");
echo("Target Encoding is set to: $this->targetEncoding<br>\n");
}
# disable case folding
if ($this->caseFold == 1) {
xml_parser_set_option($this->xmlParser, XML_OPTION_CASE_FOLDING, false);
}
# set callback functions
xml_set_element_handler($this->xmlParser, "startElement", "endElement");
xml_set_character_data_handler($this->xmlParser, "characterData");
}
function doapycss($file, $debug)
{
$this->debug = $debug;
$this->file = $file;
if (!($this->fp = fopen($this->file, "r"))) {
die("Cannot open XML data file: $this->file");
}
# read and parse data
while ($this->data = fread($this->fp, 4096)) {
# error handling
if (!xml_parse($this->xmlParser, $this->data, feof($this->fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xmlParser)),
xml_get_current_line_number($this->xmlParser)));
xml_parser_free($this->xmlParser);
}
}
}
function free()
{
# free up the parser
xml_parser_free($this->xmlParser);
}
}
$doapy = new xmlparser("travel.xml", 0);
$doapy->init();
?>
<Recordset>
<Travelpackage name="a">
<Country_name>Cuba</Country_name>
<City>Cayo Coco</City>
<Resort>Club Tryp Cayo Coco</Resort>
<Resort_rating>4</Resort_rating>
<Resort_typeofholiday>beach</Resort_typeofholiday>
<Resort_watersports>true</Resort_watersports>
<Resort_meals>true</Resort_meals>
<Resort_drinks>true</Resort_drinks>
<Package>
<Package_dateofdep>5/8/98</Package_dateofdep>
<Package_price>879</Package_price>
</Package>
</Travelpackage>
<Travelpackage name="b">
<Country_name>Cuba</Country_name>
<City>Varadero </City>
<Resort>Sol Club Paleras</Resort>
<Resort_rating>3</Resort_rating>
<Resort_typeofholiday>beach</Resort_typeofholiday>
<Resort_watersports>false</Resort_watersports>
<Resort_meals>true</Resort_meals>
<Resort_drinks>false</Resort_drinks>
<Package>
<Package_dateofdep>5/1/98</Package_dateofdep>
<Package_price>779</Package_price>
</Package>
</Travelpackage>
</Recordset>
Warning: xml_parse(): supplied argument is not a valid XML Parser resource in htdocs/doapycss/class.php on line 105
Warning: xml_get_error_code(): supplied argument is not a valid XML Parser resource in htdocs/doapycss/class.php on line 107
Warning: xml_get_current_line_number(): supplied argument is not a valid XML Parser resource in htdocs/doapycss/class.php on line 108
XML error: at line 0
Thanks very much