HCProfessionals Posted January 5, 2012 Share Posted January 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/ Share on other sites More sharing options...
requinix Posted January 5, 2012 Share Posted January 5, 2012 Certainly possible. Start with SimpleXML. Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/#findComment-1304371 Share on other sites More sharing options...
HCProfessionals Posted January 5, 2012 Author Share Posted January 5, 2012 I know PHP, but I can't wrap my head around parsing XML. Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/#findComment-1304373 Share on other sites More sharing options...
requinix Posted January 5, 2012 Share Posted January 5, 2012 You don't have to parse it yourself. SimpleXML will do that. If you don't know how to use it, check the manual for examples. Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/#findComment-1304378 Share on other sites More sharing options...
sandeep529 Posted January 5, 2012 Share Posted January 5, 2012 hi, 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/ http://crxml.pagodabox.com/ You can download the archive at http://crxml.pagodabox.com/crxml.tar 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 var_dump($crxml->search('visibility')); will output array with 1 element. array(1) { [0]=> array(7) { ["nodeName"]=> string(10) "visibility" ["accessStatement"]=> string(85) "...->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility" ["nodeChildrenCount"]=> int(1) ["nodeType"]=> int(1) ["namespaceURI"]=> NULL ["nodeContent"]=> string(52) "<visibility units="statute miles">10.00</visibility>" ["htmlEncodedNodeContent"]=> 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 http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html That is not meant to scare you. But it shows how to access xml nodes when namespaces are involved Good Luck..... Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/#findComment-1304392 Share on other sites More sharing options...
sandeep529 Posted January 5, 2012 Share Posted January 5, 2012 I am sorry, but the docuementation for the above class CRXML at crxml.pagodabox.com in under construction... please refer to http://www.phpclasses.org/browse/file/34394.html for complete documentation. Sandeep... Quote Link to comment https://forums.phpfreaks.com/topic/254381-reading-xml-with-php/#findComment-1304395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.