We never stop looking ... ...for a better way

Welcome to Woods Applied Technologies
Saturday, September 04 2010 @ 02:24 AM MDT

XML and RSS parser

research libraryfrom builder.com

http://builder.com.com/5100-6374-5109834.html?tag=rss#Listing%20C

Great article on parsing RSS....Listing C: Parsing the RSS file


<?php
// Global variables for function use.
$GLOBALS['title'] = false;
$GLOBALS['link'] = false;
$GLOBALS['description'] = false;
$GLOBALS['titletext'] = null;
$GLOBALS['linktext'] = null;
$GLOBALS['desctext'] = null;

// function: startElement
// Deals with the starting element
function startElement( $parser, $tagName, $attrs ) {
// By setting global variable of tag name
// I can determine which tag I am currently
// parsing.
switch( $tagName ) {
case 'TITLE':
$GLOBALS['title'] = true;
break;
case 'LINK':
$GLOBALS['link'] = true;
break;
case 'DESCRIPTION':
$GLOBALS['description'] = true;
break;
}
}

// function: endElement
// Deals with the ending element
function endElement( $parser, $tagName ) {
// By noticing the closing tag,
// I can print out the data that I want.
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
// Deals with the character elements (text)
function charElement( $parser, $text ) {
// Verify the tag that text belongs to.
// I set the global tag name to true
// when I am in that tag.
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 ) );
}
}

// Create an xml parser
$xmlParser = xml_parser_create();


// Set up element handler
xml_set_element_handler( $xmlParser, "startElement", "endElement" );


// Set up character handler
xml_set_character_data_handler( $xmlParser, "charElement" );


// Open connection to RSS XML file for parsing.
$fp = fopen( "http://news.com.com/2547-1_3-0-5.xml", "r" )
or die( "Cannot read RSS data file." );

// Parse XML data from RSS file.
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
}


// Close file open handler
fclose( $fp );


// Free xml parser from memory
xml_parser_free( $xmlParser );

?>

Story Options

Trackback

Trackback URL for this entry: http://www.watcorp.com/trackback.php?id=20050713205831443

No trackback comments for this entry.
XML and RSS parser | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.