I am trying to create a script to caching a remote XML files. The problem is that this XML file was poorly constructed and I can’t do anything about it.
XML file: http://www.boj.org.jm/uploads/tbills.xml
I would like to have the cached xml file to be restructured like this:
<tbills>
<tbill>
<title>Announcement</title>
<doc>www.boj.org.jm/pdf/tbill_press_release_2010-07-13.doc</doc>
<date>Jul 14 2010 12:00am</date>
</tbill>
<tbill>
<title>Results</title>
<doc>www.boj.org.jm/pdf/tbill_results_2010-june-23.doc</doc>
<date>Jun 23 2010 12:00am</date>
</tbill>
</tbill>
I can find tutorials that works if the source XML data is properly structured, but I can never seem to find one that addresses situations like this.
I found this code in a tutorial and tried to edit it to what I would need, but I’m stuck with 2 things.
[ol][li]How do I get this to change the structure of the XML when caching[/li]
[li]How do I use the call script and where do I put it?[/li][/ol]
Here’s what I’m done so far… My Caching script (caching.php)
[php]
<?php
/*
* Caching A small PHP class to
*/
class Caching {
var $filePath = "";
var $apiURI = "";
function __construct($filePath, $apiURI) {
//check if the file path and api URI are specified, if not: break out of construct.
if (strlen($filePath) > 0 && strlen($apiURI) > 0) {
//set the local file path and api path
$this->filePath = $filePath;
$this->apiURI = $apiURI;
//does the file need to be updated?
if ($this->checkForRenewal()) {
//get the data you need
$xml = $this->getExternalInfo();
//save the data to your file
$this->stripAndSaveFile($xml);
} else {
//no need to update the file
return true;
}
} else {
echo "No file path and / or api URI specified.";
return false;
}
}
function checkForRenewal() {
//set the caching time (in seconds)
$cachetime = (60 * 60 * 24 * 1); //one day worth of seconds
//get the file time
$filetimemod = filemtime($this->filePath) + $cachetime;
//if the renewal date is smaller than now, return true; else false (no need for update)
if ($filetimemod < time()) {
return true;
} else {
return false;
}
}
function getExternalInfo() {
if ($xml = @simplexml_load_file($this->apiURI)) {
return $xml;
} else {
return false;
}
}
function stripAndSaveFile($xml) {
//put the artists in an array
$tbill = $xml->TBILLS->ANNOUNCE;
//building the xml object for SimpleXML
$output = new SimpleXMLElement("");
//get only the top 10
for ($i = 0; $i < 10; $i++) {
//create a new artist
$insert = $output->addChild("announce");
//insert name and playcount childs to the artist
$insert->addChild("link", $tbill[$i]->LINK);
$insert->addChild("date", $tbill[$i]->DATE);
}
//save the xml in the cache
file_put_contents($this->filePath, $output->asXML());
}
}
?>
[/php]
My Calling script (calling.php)
[php]
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include('caching.php');
$caching = new Caching($_SERVER['DOCUMENT_ROOT']."/boj/tbills.xml",
"http://www.boj.org.jm/uploads/tbills.xml");
?>
[/php]
If anyone can help, please… I will sincerely appreciate it.
Thanks in advance.
Best Regards,
Winchester