Jump to content

Restructing an XML file while Caching


WStudio

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.