PDA

View Full Version : Saving and editing XML


thehappyappy
07-09-2007, 02:04 PM
I have a php query string (http://stats.probability.tv/feed.php?key=<MY PASSWORD>). When I go to it an xml is generated. Is it possible to get the information contained in that xml and then save it to another one?:confused:

The php is on one server and I can't access or modify that, but my site is on another server and is built with asp.net c#. It uses a RadRotator to scroll the content of an xml file through the page. At the moment I'm changing the content of the xml manually, but this changes all the time and I can't keep up with it.

Thanks
Lucy

AndyArmstrong
08-07-2007, 12:59 PM
There is no reason why you cannot do this as there is a DOM parser for those languages, use it!

thehappyappy
08-07-2007, 01:09 PM
Thanks for the advice. I have a PHP file to retrieve the xml from the feed, transform it with an xsl and then resave it as another xml file, but it just doesn't seem to be working. It isn't saving the new xml file.

AndyArmstrong
08-07-2007, 02:59 PM
can you show me the code please?

thehappyappy
08-07-2007, 03:06 PM
Thanks, here's the code of the php file:<?php
//Your files. The saved XSLT file, the remote XML, the location of the local copy and result.
$xmlFile = 'http://stats.probability.tv/feed.php?key=<MYPASSWORD>'; //The remote file
$xmlFileLocal = 'orignal.xml'; //The location of the local copy of the remote file
$xsltFile = 'format.xsl'; //The XSLT file
$newFile = 'final.xml'; //The location of the transformed file

//Display any PHP errors found further below (if any)
ini_set('display_errors','On');

//The definition of file_put_contents() in case it's not available which is most likely as that function was added in PHP5.0.0.
if (!function_exists('file_put_contents'))
{
if (!defined('FILE_APPEND')) {
define('FILE_APPEND', 1);
}
function file_put_contents($n, $d, $flag = false)
{
$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
$f = @fopen($n, $mode);
if ($f === false)
{
return 0;
}
else
{
if (is_array($d)) $d = implode($d);
$bytes_written = fwrite($f, $d);
fclose($f);
return $bytes_written;
}
}
}

//Save a local copy of the remote file at the specified location for a local copy
//This will also stript out extra spaces at the beginning and the end of the document
file_put_contents($xmlFileLocal, trim(file_get_contents($xmlFile)));

//Check if the XSLT extension is available
if (!function_exists('xslt_create')) {
die('The XSLT exntesion is not enabled. Contact your host to enable it.');
}
//Prepare the XSLT processor.
$xh = xslt_create();

//Execute the transformation
if (!$transformation = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', null, array('/_xml' => file_get_contents($xmlFileLocal), '/_xsl' => file_get_contents($xsltFile)))) {
die('Error during transformation of "' . $xmlFileLocal . '" with "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct.');
}
if (!file_put_contents($newFile, $transformation)) {
die('Unable to write "' . $newFile . '". Make sure the definition of file_put_contents() is available');
}

//Free the XSLT processor.
xslt_free($xh);

echo 'The script executed succesfully. A local copy of "' . $xmlFile . '" is available at "' . $xmlFileLocal . '" and a transformed by "' . $xsltFile . '" version is available at "' . $newFile . '".';
?>

AndyArmstrong
08-07-2007, 04:19 PM
Thats a very complictated long looking if statement,

if (!$transformation = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', null, array('/_xml' => file_get_contents($xmlFileLocal), '/_xsl' => file_get_contents($xsltFile)))) {
die('Error during transformation of "' . $xmlFileLocal . '" with "' . $xsltFile . '" as stylesheet. Possibly an endless loop or illegal XSLT/XPath construct.');
}

It wouldnt surprise me if the if is false everytime then the next if is false,when maybe it shouldntbe?

thehappyappy
08-07-2007, 04:26 PM
I don't know if it makes any difference, but I'm using PHP 4.4.4 and am very new to PHP. Somebody helped me with the file so I don't quite understand what you mean.