Jump to content

Reading XML with PHP


Recommended Posts

I just can't wrap my head around it, but I am trying to integrate weather into my website using NOAA.


1. Here is the NOAA page: http://forecast.weather.gov/MapClick.php?CityName=Mio&state=MI&site=APX&lat=44.663&lon=-84.1446

2. Here is the XML to that page: http://forecast.weather.gov/MapClick.php?lat=44.66300&lon=-84.14460&FcstType=dwml


Now what I want to do is to have PHP on my website that reads the XML from the NOAA site and create the "Forecast At A Glance". Possible or too challenging?

Link to comment
Share on other sites



Your XML contains a bit of namespaces, So if you find simpleXML difficult, you can try using a class I created. Which makes  accessing CDATA sections and namespaces easy/




You can download the archive at 



The advantage of this class is that you have a search function. You can call it with a name 'visibility' and it will return the php statement using this class that you can use to access the value of that node.


for example after loading your xml into the class , the statemment




will output  array with 1 element.


array(1) {
  array(7) {
    string(10) "visibility"
    string(85) "...->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"
    string(52) "<visibility units="statute miles">10.00</visibility>"
    string(74) "<visibility units="statute miles">10.00</visibility>"


In the above section there is a "accessStatement" element....this contains value "->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"


so to access the value of that node from php you can use


echo $crxml->->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility;


will echo 10.


to get the 'Units' attribute for visibility  you can give as


echo $crxml->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility['units']


will echo 'statute miles'...


This way you dont have to manually find your way through the XML.  What ever you can do with simpleXML like iterating, You can do with this class also.


I have to warn you that even though I have somewhat tested and is activity developing this class, I am the only one working on this.


So you might want to try simplexml first. Even though it seems a bit hard to me, people have been using it for long, and there will be solutions in the net for what ever issue you come up with.


Or try using DOM XML functions. Its What I have used to make the above class. I Have found it much more consistent and powerful.


If you use DOM function, you better take a look at XPath also http://www.tizag.com/xmlTutorial/xpathtutorial.php.


If you choose to use simpleXML pls see the link




That is not meant to scare you. But it shows how to access xml nodes when namespaces are involved


Good Luck.....

Link to comment
Share on other sites


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.