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;
}
<?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>";
?>