Jump to content

How to use PHP to parse an XML file


redesigner

Recommended Posts

I'm having some difficulty figuring out how to use PHP to parse an XML file and display certain values from the XML.  I've had no trouble using cURL to copy the particular XML feed in question to my server.  I'm doing that using this bit of PHP in the <head> section of my index.php file:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=LGA');
$fp = fopen('current_obs.xml', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
?>

 

I've tried using JavaScript to pull the data from the weather, temp_f and relative_humidity nodes of the XML file.  This is the script in the body section of index.php:

 

<script type="text/javascript">
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest()
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
xhttp.open("GET","current_obs.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

document.getElementById("temp_f").innerHTML=xmlDoc.getElementsByTagName("temp_f")[0].childNodes[0].nodeValue;
document.getElementById("weather").innerHTML=xmlDoc.getElementsByTagName("weather")[0].childNodes[0].nodeValue;
document.getElementById("relative_humidity").innerHTML=xmlDoc.getElementsByTagName("relative_humidity")[0].childNodes[0].nodeValue;
</script>

 

When I try and call the values using this bit of HTML, nothing happens:

 

<div id="weatherblock">
<span id="weatherheader">Currently in Brooklyn</span><br />
<span id="temp_f"></span> °F<br />
<span id="weather"></span><br />
<span id="relative_humidity"></span>% humidity
</div>

 

There are no errors--just no output when the browser renders index.php (aside from the "ºF" and "% humidity" bits appearing).  Can anyone tell me where I'm going wrong, or suggest a better (simple) way to do this?

Link to comment
https://forums.phpfreaks.com/topic/201827-how-to-use-php-to-parse-an-xml-file/
Share on other sites

I think I've made some progress here.  The cURL portion of my page still seems to be working just fine:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=LGA');
$fp = fopen('current_obs.xml', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
?>

 

Also, I've replaced all that JavaScript nonsense with SimpleXML commands written in PHP that should, as far as I can tell, anyway, work:

 

<?php
$xml = simplexml_load_file('current_obs.xml');
print "<span id=\"temp_f\">{$xml->current_observation[0]->temp_f}</span><span id=\"fahrenheit\"> °F</span><br />";
print "<span id=\"currently\">{$xml->current_observation[0]->weather}</span><br />";
print "<span id=\"relative_humidity\">{$xml->current_observation[0]->relative_humidity} relative humidity</span>";
?>

 

However, something isn't quite working right.  The script runs and outputs this:

 

<span id="temp_f"></span><span id="fahrenheit"> °F</span><br />
<span id="currently"></span><br />
<span id="relative_humidity"> relative humidity</span>

 

So the print command is working but for some reason the XML values aren't being inserted.  Any idea what could be going awry?

Solved it!  For some reason, including the 'current_observation' node was causing the problem.  This is the code that worked:

 

<?php
$xml = simplexml_load_file('current_obs.xml');
print "<span id=\"temp_f\">{$xml->temp_f}</span><span id=\"fahrenheit\"> °F</span><br />";
print "<span id=\"currently\">{$xml->weather}</span><br />";
print "<span id=\"relative_humidity\">{$xml->relative_humidity} relative humidity</span>";
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.