XML Parsing: What is wrong with my code?
Posted: Fri May 28, 2010 5:21 pm
My browser is showing absolutely nothing... a blank page.
What's wrong with my code?
The XML file can be found here: http://www.meltwaternews.com/magenta/xm ... 9.html.XML
It is also listed inside the code above.
What's wrong with my code?
Code: Select all
<?php
$xml_file = "http://www.meltwaternews.com/magenta/xml/html/37/10/147839.html.XML";
$xml_title_key = "*DOCUMENTS*DOCUMENT*TITLE";
$xml_url_key = "*DOCUMENTS*DOCUMENT*URL";
$xml_igress_key = "*DOCUMENTS*DOCUMENT*INGRESS";
$xml_date_key = "*DOCUMENTS*DOCUMENT*CREATEDATE5";
$story_array = array();
$counter = 0;
class xml_document{
var $title, $url, $ingress, $date;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_title_key, $xml_url_key, $xml_ingress_key, $xml_date_key, $counter, $document_array;
switch($current_tag){
case $xml_title_key:
$document_array[$counter] = new xml_document();
$document_array[$counter]->title = $data;
break;
case $xml_url_key:
$document_array[$counter]->url = $data;
$counter++;
break;
case $xml_ingress_key:
$document_array[$counter]->ingress = $data;
$counter++;
break;
case $xml_date_key:
$document_array[$counter]->date = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, 80000) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>Project: Parse XML 2</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for($x=0;$x<count($document_array);$x++){
echo "\t<h2>" . $document_array[$x]->title . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $document_array[$x]->url . "</i>\n";
echo "\t\t\n";
echo "\t<i>" . $document_array[$x]->ingress . "</i>\n";
echo "\t\t\n";
echo "\t<i>" . $document_array[$x]->date . "</i>\n";
}
?>
</body>
</html>
It is also listed inside the code above.