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
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: