Page 1 of 1

Simple XML to HTML

Posted: Thu Nov 06, 2008 5:18 pm
by okelly
hello everybody

Not PHP I Know...but....
Can anybody point out to me why the code below is failing to render the xml file here http://www.ecb.europa.eu/stats/eurofxre ... -daily.xml or alternatively point me to a simple php solution to consume the xml file http://www.ecb.europa.eu/stats/eurofxre ... -daily.xml and render it to a HTML table on a web page? I know its simple but its 11pm here! I'm just getting a blank page with the code below.
I just need a simple table with the currency and corresponding rate rendered in a html table. Appreciate the code below is js, not php. I'm open to a php solution!

thnx very much

Code: Select all

 
<html>
<body>
 
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
 
document.write("<table border='1'>");
 
var x=xmlDoc.getElementsByTagName("Cube");
for (i=0;i<x.length;i++)
{ 
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("rate")[0].childNodes[0].nodeValue);
document.write("</td>");
 
document.write("<td>");
document.write(
x[i].getElementsByTagName("currency")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
 
</body>
</html>
 
 

Re: Simple XML to HTML

Posted: Fri Nov 07, 2008 2:08 am
by Rovas
Your script is wrong because it can' t load the needed file. I haven' t used JavaScript to load files like this I only worked with XMLHttpRequest, which I recommend because it simpler than what are your trying to do.
For the PHP version there are numerous examples out there using different methods (using file and regular expressions, SimpleXML, CURL) for example I work on that uses SimpleXML but I hit a problem and I don' t know how to fix it.
Here are two examples in PHP that I hope will guide you to your solution:

Code: Select all

 
   //this is not an elegant solution but it works, I made it base on a script that I already made
   $url="your url";
   $course=file($url);
  for ($i=7; $i<13;$i++){
   //to have the format currency name value
   echo "<div>" .substr($course[$i],16,3) .substr($course[$i], 22,6) ."</div>"; 
  }
  //second example not mine, made by danieLs@skullbox.info, which is a comprehensive solution. 
  //
function GetCurrency()
{
    $xmlUrl = "your_url";
    $cacheFile = './exchangeRate.cache';
    $timedif = @(time() - filemtime($cacheFile)); 
    $result = array();
    
    if ($timedif < 3600)
    {
        $result = unserialize(join('', file($cacheFile)));
    }
    else
    {
        if ($f = @fopen($xmlUrl, 'r')) 
        {
            while (!feof($f)) 
            {
                $line = fgets($f, 4096);
                if (preg_match("/(\s)?<Cube currency='([A-Z]{3})' (multiplier='100')?>([0-9\.]+)<\/Cube>/", $line, $out))
                {
                    $result = array_merge($result, array($out[2] => $out[4]));
                }        
            }           
            fclose($f);           
            $serialized = serialize($result);          
            if ($f = @fopen($cacheFile, 'w')) 
            {
                fwrite($f, $serialized, strlen($serialized));
                fclose($f);
            }
        }
    }
    return $result;    
}
 
print_r (GetCurrency(););
?>