Free web development scripts for the webmasterDevelopment Scripts


 social media marketing services
 Site Navigation
Clicky Web Analytics
 o Development Scripts
 o Text Only Edition
 o PHP.net News
 o Tutorials
 o Register an account
 o New entries this week
 o Affiliates/Links
 o Contact Us
 o About us
 o Advanced Search

 Browse Path
 o 1 user online
 o Most users online: 67
 o Home
  o PHP
   o XML
    o Script

 Member Login
User Name
Password

 Standards
Valid XHTML 1.0!
Valid 
CSS!

 Recommended Links
 o PHP Freaks
 o Paper Free Invoicing
 o Tutorials
 o Expired Domain Crawler

XML Parsing Class (Rated 4.5)

Description:

Quick and dirty XML parsing class, as used at OxyScripts.com to parse the news feeds from other sites. It works as it should, although the coding is dirty. It seems to run quite quickly too !

Code starts here


<?php
/* USAGE
$parser = new parser;
$fp = $parser->getXML("http://www.domain.com/file.xml", "file.xml");

while ($data = fread($fp, 4096)) {
$parser->parse($data);
}
$parser->close();

*/

class parser {

var
$insideitem = false;
var
$tag = "";
var
$title = "";
var
$description = "";
var
$link = "";

function
parser() {
$this->parser = xml_parser_create();
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "characterData");
return
$this->parser;
}

function
parse($data) {
xml_parse($this->parser, $data, feof($this->fp));
}

function
getXML($source, $target) {

$time = time() - 21600; // 6 hours in seconds, change this as needed

if(filemtime($target) < $time) {
$fp = fopen($source, 'r');
$fd= fopen($target, 'w');
fclose($fd);
while (!
feof($fp)) {
$buffer = fgets($fp, 4096);
$fd= fopen($target, 'a');
fputs($fd, $buffer);
fclose ($fd);
}

fclose($fp);
}
$this->fp = fopen($target,"r") or die("Error reading RSS data.");

return
$this->fp;
}

function
startElement($parser, $tagName, $attrs) {
if (
$this->insideitem) {
$this->tag = $tagName;
} elseif (
$tagName == "ITEM") {
$this->insideitem = true;
}
}

function
endElement($parser, $tagName) {
if (
$tagName == "ITEM") {
$feed['link'] = trim($this->link);
$feed['title'] = trim($this->title);
$feed['description'] = trim($this->description);
echo
"<A HREF=\"".$feed['link']."\">".$feed['title']."</A>\n<br>\n";

if(
$feed['description']) {
echo
"".$feed['description']."<br>\n<br>\n";
}

$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;
}
}

function
characterData($parser, $data) {
if (
$this->insideitem) {
switch (
$this->tag) {
case
"TITLE":
$this->title .= $data;
break;
case
"DESCRIPTION":
$this->description .= $data;
break;
case
"LINK":
$this->link .= $data;
break;
}
}
}

function
close() {
fclose($this->fp);
xml_parser_free($this->parser);
$this->fp = '';
$this->parser = "";
}
}
?>

Last Edited: 2003-02-11 22:37:31


Submitted by Oxy on 12-02-2003 22:29


Recent Additions (from all categories)

(PHP) T Shirt Printing Software

(PHP) Youtube Video Organizer Script

(PHP) MP3z - MP3 Search Engine Script

(PHP) Recipez- PHP Recipe Script

(ASP) Stop image hotlinking and downloading. Web image protection is easy with Image Trapper.

Top Rated Scripts

(Javascripts) Javascript Delayed Redirection Rating: 5

(PHP) Random Links and images with PHP & MySQL Rating: 5

(PHP) RSS/XML news feed headline grabber! Rating: 5

(PHP) Random Signatures/Avatars Rating: 5

(Javascripts) Detecting Client Web Browser Rating: 5

© Copyright 2003 - Devscripts.net  Web Analytics