Creating an RSS Reader: the Reader - XML-related Functions (
Page 2 of 4 )
Now, PHP provides us with several XML-related functions, a few of which we will be using here:
xml_parser_create() – Creates an instance of the xml parser object. Xml_parser_create() is a class. In order to use any class we need to instantiate it, or create a copy of it.
To create a new copy:
$xmlParser = xml_parser_create();
xml_set_element_handler() – Searches and sets the start and end elements(tags). This function sets the start and end tags for the parser. It accepts three parameters:
- The parser: references the parser that is calling the handler.
- The tagname: contains the name of the element for which the handler is called.
- The attributes: an array that contains the element's attributes.
The parameters are used later in this article.
xml_set_character_data_handler() – This handles the text part of the tag elements. This function takes two parameters, the parser and data.
- The parser: references the parser that is calling the handler.
- The data: contains the character data as a string.
You can get more information about these and other XML functions at:
http://uk2.php.net/manual/en/ref.xml.php
The first thing we do is set the global variables that are going to be used by the functions.
$GLOBALS['titletag'] = false;
$GLOBALS['linktag'] = false;
$GLOBALS['descriptiontag'] = false;
$GLOBALS['thetitletxt'] = null;
$GLOBALS['thelinktxt'] = null;
$GLOBALS['thedesctxt'] = null;
These variables are going to be used to read in tag information from the RSS file that is going to be used with this reader.
The function below deals with the starting element. This function searches through the document to find one of the three tags we discussed earlier:
function startTag( $parser, $tagName, $attrs ) {
switch( $tagName ) {
case 'TITLE':
$GLOBALS['titletag'] = true;
break;
case 'LINK':
$GLOBALS['linktag'] = true;
break;
case 'DESCRIPTION':
$GLOBALS['descriptiontag'] = true;
break;
}
}
This next function deals with the end tag:
function endTag( $parser, $tagName ) {
switch( $tagName ) {
case 'TITLE':
echo "<p><b>" . $GLOBALS[the'titletxt'] . "</b><br/>";
$GLOBALS['titletag'] = false;
$GLOBALS['thetitletxt'] = "";
break;
case 'LINK':
echo "Link: <a href="". $GLOBALS['thelinktxt'] . "">" .
$GLOBALS['thelinktxt'] . "</a><br/>";
$GLOBALS['linktag'] = false;
$GLOBALS['thelinktxt'] = "";
break;
case 'DESCRIPTION':
echo "Desc: " . $GLOBALS['thedesctxt'] . "</p>";
$GLOBALS['descriptiontag'] = false;
$GLOBALS['thedesctxt'] = "";
break;
}
}
This next function verifies the tag that the text belongs to. Once we know which tag it is that we are dealing with, we set the global variable to true.
function txtTag( $parser, $text ) {
if( $GLOBALS['titletag'] == true ) {
$GLOBALS['thetitletxt'] .= htmlspecialchars( trim
($text) );
} else if( $GLOBALS['linktag'] == true ) {
$GLOBALS['thelinktxt'] .= trim( $text );
} else if( $GLOBALS['descriptiontag'] == true ) {
$GLOBALS['thedesctxt'] .= htmlspecialchars( trim
( $text ) );
}
}