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??
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.