PDA

View Full Version : XML parser in PHP problem



Greggeh
February 20th, 2007, 10:26 AM
Ok so I did the xml parsing tutorial here (http://www.kirupa.com/web/xml_php_parse_intermediate.htm (http://www.kirupa.com/forum/../web/xml_php_parse_intermediate.htm))and got everything to work.

Now i want to make my XML file an RSS file so people can subcribe to the file. Now the problem is: As soon as i put
<rss version="2.0">

underneath

<?xml version="1.0"?>

the php file cant read the xml file anymore.

Does anyone know whats the problem here?

Greggeh
February 21st, 2007, 10:13 AM
Ok maybe this will get you started


this is the original xml file:
<?xml version="1.0"?> <news> <story> <headline> Godzilla Attacks LA! </headline> <description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description> </story></news>and this is how i want it:

<?xml version="1.0"?>
<rss version="2.0">
<news> <story> <headline> Godzilla Attacks LA! </headline> <description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description> </story></news>
and this is the php file to read the xml file:


<?php

$xml_file = "xml_intermediate.xml";

$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";

$story_array = array();

$counter = 0;
class xml_story{
var $headline, $description;
}

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_headline_key, $xml_description_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $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, filesize($xml_file)) 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);

?>

Greggeh
February 26th, 2007, 05:17 AM
*bump

any ideas anyone please?