Jump to content

PHP XLM PARSE "XML Error: syntax error at line 1"


Malcerous

Recommended Posts

I am using the Turorial off of PHP Freaks [a href=\"http://www.phpfreaks.com/tutorial_cat/28/PHP-XML.php\" target=\"_blank\"]PHPFreaks Tutorial[/a]

[code]
<?PHP
function print_error() {
    global $parser;
    die(sprintf("XML Error: %s at line %d",
        xml_error_string(xml_get_error_code($parser)),
        xml_get_current_line_number($parser)
    ));
}    

//create xml parser object
$parser = xml_parser_create();

//this option ensures that unneccessary white spaces
//between successive elements would be removed
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);

//to use XML code correctly we have to turn case folding
//(uppercasing) off. XML is case sensitive and upper
//casing is in reality XML standards violation
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);

//read XML file into $data
$data = implode("",file('http://sports.yahoo.com/nhl/rss.xml'));

//parse XML input $data into two arrays:
//$i_ar - pointers to the locations of appropriate values in
//$d_ar - data value array
xml_parse_into_struct($parser,$data,&$d_ar,&$i_ar) or print_error();

//to view content of $d_ar and/or $i_ar uncomment lines below
//echo '<pre>';
//print_r($d_ar);
//print_r($i_ar);

//cycle all <item> tags.
//$i_ar['item'] contains all pointers to <item> tags
for($i=0; $i<count($i_ar['item']); $i++) {
    //since we have <item> nested inside another <item> tag,
    //we have to check if pointer is to open type tag.
    if($d_ar[$i_ar['item'][$i]]['type']=='open') {
        //now for all content within single <item> element
        //extract needed information
        for($j=$i_ar['item'][$i]; $j<$i_ar['item'][$i+1]; $j++) {
            if($d_ar[$j]['tag'] == 'caption') {
                $caption = $d_ar[$j]['value'];
            }elseif($d_ar[$j]['tag'] == 'url') {
                $url = $d_ar[$j]['attributes']['value'];
            }
        }
        //output link
        echo '<a href="'.$url.'">'.str_repeat('=',$d_ar[$j]['level']-1).$caption.'</a><br>';
    }
}

//unseting XML parser object
xml_parser_free($parser);

/*
*tab-width=4      
*indent=4
*width=90
*/
?>
[/code]

I saved the file as rss.php and goto the page and get the following error.

XML Error: syntax error at line 1

Does anyone have any ideas why I am getting this meaage?
Can you please test this script on your site to see it it works??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.