|
|
|
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>";
?> | | |
|
| 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) | | | XMLRPC server class and handler Categories : PHP, PHP Classes, XML | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | | | Directory Listing To XML : Outputs XML File of a Given Directory Listing Categories : PHP, PHP Classes, XML, Directories | | | Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects. Categories : PHP, PHP Classes, XML, Web Services | | | DBXML- A Class to backup databases in XML Format using web interface Categories : PHP, PHP Classes, Databases, MySQL, XML | | | 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 | | | XML easy parser Categories : PHP, XML, PHP Classes | | | [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 | | | XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | News management class Categories : PHP, PHP Classes, Beginner Guides | |
|
|
|