XML To Article Generator

Friday 9 January 2009

Still toying around with ideas of a content generator template generator command center, here is a few ideas for an article generator.

Idea: RSS feeds! Not the fastest if pulled on the fly, but providing the xml sources are researched properly to ensure that relevant articles appear, and that they are actually updated often, with just a few lines of code you are able to manipulate the data as easy as a drunk college chick!

Save this as xml.php

<?php
/************************************************************************
* Copyright 2005 Niklas Angebrand <niklas.a@gamershell.com>            *
************************************************************************
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or    *
* (at your option) any later version.                                  *
*                                                                      *
* This program is distributed in the hope that it will be useful,      *
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU General Public License for more details.                         *
************************************************************************/
class RSSParser {
var $in_item, $tag, $item, $items, $parser, $rss_data, $error;
function RSSParser($rss_file = "http://hwhell.com/xml/feature_feed.rss") {
  $this->rss_data = file_get_contents($rss_file);
  $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');
}

function parse() {
  if (!xml_parse($this->parser, $this->rss_data, true)) {
    $this->error = xml_error_string(xml_get_error_code($this->parser)).": ".xml_get_current_line_number($this->parser);
    return false;
  }
  return $this->items;
}

function startElement($parser, $tagname, $attrs) {
  if ($this->in_item) {
    $this->tag = $tagname;
  } else if ($tagname == 'ITEM') {
    $this->in_item = true;
  }
}

function endElement($parser, $tagname) {
  $this->tag = '';
  if ($this->in_item && $tagname == 'ITEM') {
    unset($this->item['']);
    $this->items[] = $this->item;
    $this->in_item = false;
  }
}

function characterData($parser, $data) {
  $this->item[$this->tag] = $data;
}
}

?>

This handy little class, while looking to scrape gamershell, found I did.

There is loads of ways of parsing xml, but this little puppy has served me well for over a year, and my only mods being removing some whitespace. I just use this for the simple fact this class makes an appearance a few times in my development and construction needs!

To output the feed as some kind of article:

<?php
include'xml.php';// the xml class
$rss = new RSSParser('http://www.yourfeedsource.com/xml.xml');// rss feed
$items = $rss->parse();
$i = 0;// counter
echo'<p>';
foreach ($items as $item):
 echo $item['DESCRIPTION'];
 $i % 3 == 0 ? print"</p>\n<p>" : '';// create our own paragraphs
 $i++;
endforeach;
echo"</p>\n";
?>

All we are doing here is stripping the feed items down to nothing but the descriptions, then making paragraphs out of them. There you go, one fresh article! Depending on your sources, you could have some pretty decent, and elligible results that may even fly under the radar!