WStudio Posted August 31, 2010 Share Posted August 31, 2010 I can trying to create a script for caching an 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. [*]How do I get this to change the structure of the XML when caching How do I use the call script and where do I put it? [*] Caching script (caching.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); //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->ANNOUCE; //building the xml object for SimpleXML $output = new SimpleXMLElement("<tbill><title></title></tbill>"); //get only the top 10 for ($i = 0; $i < 10; $i++) { //create a new artist $insert = $output->addChild("artist"); //insert name and playcount childs to the artist $insert->addChild("name", $artists[$i]->name); $insert->addChild("playcount", $artists[$i]->playcount); } //save the xml in the cache file_put_contents($this->filePath, $output->asXML()); } } ?> Calling script (calling.php) <?php ini_set('display_errors', 1); error_reporting(E_ALL); include('caching.php'); $caching = new Caching($_SERVER['DOCUMENT_ROOT']."/tbills.xml", "http://www.boj.org.jm/uploads/tbills.xml"); ?> Thanks in advance. Best Regards, _________ Winchester Link to comment https://forums.phpfreaks.com/topic/212124-restructing-an-xml-file-while-caching/ Share on other sites More sharing options...
WStudio Posted August 31, 2010 Author Share Posted August 31, 2010 Oppps.. that was the wrong version of the caching.php script. Here's the right one <?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("<announce></announce>"); //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()); } } ?> Link to comment https://forums.phpfreaks.com/topic/212124-restructing-an-xml-file-while-caching/#findComment-1105454 Share on other sites More sharing options...
WStudio Posted August 31, 2010 Author Share Posted August 31, 2010 Hi Again, Any help will be appreciated. Thanks Best Regards, _________ Winchester Link to comment https://forums.phpfreaks.com/topic/212124-restructing-an-xml-file-while-caching/#findComment-1105669 Share on other sites More sharing options...
WStudio Posted September 1, 2010 Author Share Posted September 1, 2010 [bumped] Help Please?? :'( Link to comment https://forums.phpfreaks.com/topic/212124-restructing-an-xml-file-while-caching/#findComment-1106212 Share on other sites More sharing options...
WStudio Posted September 3, 2010 Author Share Posted September 3, 2010 If this is not the right way to go about this or it just simply cannot be done, will some body tell me this please? If it can be done or there is a better/easier way to do this, can someone please point me to where I can find the information (if it is available. Will someone just say something please!!! Link to comment https://forums.phpfreaks.com/topic/212124-restructing-an-xml-file-while-caching/#findComment-1106669 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.