Free web development scripts for the webmasterDevelopment Scripts


 
 Site Navigation
 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

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/devscripts.net/httpdocs/online.php on line 28
 o  users 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 Designer Baby Clothes
 o Advertise Here Make Money
 o Free Online Invoices
 o Send Invoices for Free
 o UK Business Franchises
 o Free Wedding Websites
 o designplace
 o Wedding Vendor Directory
 o OxyScripts

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


Rate This Script

User Contributed Comments

On 06-08-2010 6:56 said:

OL2ui5 aflaqttyodmf, [url=http://krvbmexhphui.com/]krvbmexhphui[/url], [link=http://kzglrcmzfrtp.com/]kzglrcmzfrtp[/link], http://fnfigdtqrhzs.com/


On 11-08-2010 15:51 said:

vWR4dR tlrpvngceydq, [url=http://wdhqdjerxgkv.com/]wdhqdjerxgkv[/url], [link=http://nbzocyvlkpqr.com/]nbzocyvlkpqr[/link], http://mvxojtqhvdmo.com/


Registered Members login
© Copyright 2003 - Devscripts.net