WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A very basic and fast XML parser
Categories : PHP, PHP Classes, XML Click here to Update Your Picture
Ioannis Cherouvim
Date : May 25th 2005
Grade : 2 of 5 (graded 6 times)
Viewed : 19630
File : 4174.php
Images : No Images for this code example.
Search : More code by Ioannis Cherouvim
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

title : A very basic and fast XML parser

story : Two objects are included here, a reader and a parser. All you need to do is instantiate
the xmlParser object and let it do the job for you. nextToken() will return the next
token, which can be a tag or the text between 2 tags. the isTag() will determine if this
is a tag so you can do something smart with it. Lastly you can jumpTo($tagname) if you
know the name of the tag you want to skip to. This can be "<p>" or something like "<div
class='menuItems'>" Run the example and enjoy.

author : Ioannis Cherouvim
e-mail : morales@hack.gr
date : 2005-05-25


Class : fileReader
<?php
class fileReader {
    var
$file;
    var
$filename;
    var
$step = 2048;
    var
$bytesRead = 0;
    var
$pointer = 0;
    var
$buffer = "";
    var
$bufferLength = 0;
    var
$eof = false;

    function
fileReader($filename) {
       
$this->filename = $filename;
    }

    function
open() {
       
$this->file = fopen ($this->filename, "rb");
    }

    function
close() {
       
fclose ($this->file);
    }

    function
getBytesRead() {
        return
$this->bytesRead;
    }

    function
eof() {
        return
$this->eof;
    }

    function
readByte() {
        if (
$this->pointer == $this->bufferLength) {
            if (!
feof($this->file)) {
               
$this->buffer = fread ($this->file, $this->step);
               
$this->bufferLength = strlen($this->buffer);
               
$this->bytesRead += $this->bufferLength;
               
$this->pointer = 0;
            } else {
               
$this->eof = true;
               
$this->close();
                return
"";
            }
        }
        return
$this->buffer[$this->pointer++];
    }
}
?>


Class : xmlParser
<?php
class xmlParser {

    function
xmlParser($filename) {
       
$this->reader = new fileReader($filename);
       
$this->open();
       
$this->ch0 = $this->reader->readByte();
       
$this->isTag = ($this->ch0 == "<");
       
$this->delim = ($this->isTag?">":"<");
       
$this->buffer = "";
    }

    function
open() {
       
$this->reader->open();
    }

    function
getBytesRead() {
        return
$this->reader->getBytesRead();
    }

    function
eof() {
        return
$this->reader->eof();
    }
   
    function
isTag() {
        return
$this->isTag;
    }

    function
feedBuffer() {
       
$this->buffer .= $this->ch0;
        if (!
$this->reader->eof()) {
           
$this->ch0 = $this->reader->readByte();
        } else {
           
$this->ch0 = "";
        }
    }

    function
nextToken() {
        while (
$this->ch0 != $this->delim && !$this->reader->eof()) {
           
$this->feedBuffer();
        }
        if (
$this->isTag) {
           
$this->feedBuffer();
        }
       
$this->isTag = ($this->ch0 == "<");
       
$this->delim = ($this->isTag?">":"<");
       
$temp = $this->buffer;

       
$this->buffer = "";

       
$this->feedBuffer();
        return
$temp;
    }

    function
jumpTo($tagname) {
       
$token = "";
        while (
strpos($token, "<$tagname") !== 0 && !$this->reader->eof()) {
           
$token = $this->nextToken();
        }
       
$this->tagFound = (strpos($token, "<$tagname") === 0);
    }

}
?>


example use
<?php
    $xml
= new xmlParser("http://wordpress.org/");

   
//get the contents of a page and display them nicely
    //tags in orange, elements in plain text
   
while(!$xml->eof()) {
        if (
$xml->isTag()) {
            echo
"<span style='background:#fa4'>".htmlentities($xml->nextToken())."</span>";
        } else {
            echo
$xml->nextToken();
        }
    }

    echo
"<hr>";

   
//parse an rss page, locate a specific tag and display the enclosed element
   
$xml2 = new xmlParser("http://rss.cnn.com/rss/cnn_topstories.rss");
   
$xml2->jumpTo("item");
   
$xml2->jumpTo("title");
    echo
"the first topic of CNN is : <span style='background:#4af'>".$xml2->nextToken()."</span>";
?>



DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
XML Menu
Categories : PHP, PHP Classes, Navigation, XML, XSL
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases
logger class (PHP5 +)
Categories : PHP, PHP Classes, Log Files, XML
XMLRPC server class and handler
Categories : PHP, PHP Classes, XML
XML easy parser
Categories : PHP, XML, PHP Classes
Directory Listing To XML : Outputs XML File of a Given Directory Listing
Categories : PHP, PHP Classes, XML, Directories
[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
XPath for PHP without the DOM XML extension
Categories : DOM XML, XML, XSLT, PHP Classes, PHP
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects.
Categories : PHP, PHP Classes, XML, Web Services
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
Class to Create protected URLs
Categories : PHP, PHP Classes, URLs
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps