<?php
$GLOBALS['title'] = false;
$GLOBALS['link'] = false;
$GLOBALS['description'] = false;
$GLOBALS['titletext'] = null;
$GLOBALS['linktext'] = null;
$GLOBALS['desctext'] = null;
function startElement( $parser, $tagName, $attrs ) {
switch( $tagName ) {
case 'TITLE':
$GLOBALS['title'] = true;
break;
case 'LINK':
$GLOBALS['link'] = true;
break;
case 'DESCRIPTION':
$GLOBALS['description'] = true;
break;
}
}
function endElement( $parser, $tagName ) {
switch( $tagName ) {
case 'TITLE':
echo "<p><b>" . $GLOBALS['titletext'] . "</b><br/>";
$GLOBALS['title'] = false;
$GLOBALS['titletext'] = "";
break;
case 'LINK':
echo "Link: <a href=\"". $GLOBALS['linktext'] . "\">" . $GLOBALS['linktext'] . "</a><br/>";
$GLOBALS['link'] = false;
$GLOBALS['linktext'] = "";
break;
case 'DESCRIPTION':
echo "Desc: " . $GLOBALS['desctext'] . "</p>";
$GLOBALS['description'] = false;
$GLOBALS['desctext'] = "";
break;
}
}
function charElement( $parser, $text ) {
if( $GLOBALS['title'] == true ) {
$GLOBALS['titletext'] .= htmlspecialchars( trim($text) );
} else if( $GLOBALS['link'] == true ) {
$GLOBALS['linktext'] .= trim( $text );
} else if( $GLOBALS['description'] == true ) {
$GLOBALS['desctext'] .= htmlspecialchars( trim( $text ) );
}
}
$xmlParser = xml_parser_create();
xml_set_element_handler( $xmlParser, "startElement", "endElement" );
xml_set_character_data_handler( $xmlParser, "charElement" );
$fp = fopen( "
http://ekstrabladet.dk/rss2/?mode=normal&submode=112", "r" )
or die( "Kan ikke læse RSS feed." );
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
}
fclose( $fp );
xml_parser_free( $xmlParser );
?>