Jump to content


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


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

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]

function print_error() {
    global $parser;
    die(sprintf("XML Error: %s at line %d",

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

//this option ensures that unneccessary white spaces
//between successive elements would be removed

//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

//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>';

//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


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??

Share this post

Link to post
Share on other sites


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.