Home arrow MySQL arrow Page 2 - Creating an RSS Reader: the Reader

XML-related Functions - MySQL

In this article we are going to discuss how to create a PHP-based RSS reader. It would be helpful if you know something about XML, but not really necessary. RSS documents have three main tags: Title, Link and Description. And they all do exactly what their names suggest. I will go into detail about these tags in my second article dealing with “building an RSS file.” For now, we will only focus on the “reading” part of the article.

TABLE OF CONTENTS:
  1. Creating an RSS Reader: the Reader
  2. XML-related Functions
  3. The Meat of the Code
  4. Reading RSS Data from a Database (Optional)
By: Jacques Noah
Rating: starstarstarstarstar / 16
February 06, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 ) );

    }

  }



 
 
>>> More MySQL Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: