So, this tale, which for your sake I will attempt to make brief, is typical of my experiences in web development. As one of my old bosses used to say (and I’m paraphrasing), “if there’s a hard way to do it, you’ll try that way first.”
To begin, I have as my personal homepage, a calendar page that is scripted with PHP and MySQL. The calendar not only displays date information, but also displays things like birthdays, Sun and Moon information, bills, and a to-do list. As I constantly visit weather sites I decided it was time to bring the weather information to my calendar. Searching an API link site I found that the NOAA provides just what I needed: NOAA Weather API. While it was fun setting up the SOAP call, the resulting information (returned) was in XML format. Not the biggest fan of XML, but it’s what we’re given to work with in this situation.
Fast forward through scripting the SOAP call, caching data locally for X amount of hours (NOAA would appreciate not being queried over once per hour), writing the initial XSL for XSLT, and wrapping it as an object for display in my calendar. Everything seemed to be fine till I decided to look at my calendar with my smart phone. Turns out the smart phone (an old one) running Android 2.2.2 wouldn’t translate the XML in it’s built in browser. I have no idea if newer phones will deal with XML (maybe with a different browser?), but that didn’t matter to me, it has to run everywhere.
Researching the subject someone pointed out that whenever possible all XML transformation should be done server-side. I understood what he was talking about, browser compatibility issues being the bane of every web developer, but that adds to server-side load, not to mention the fact that if done server-side, there’s not much advantage of using XML in the first place (for information transmission). One of the perks (perquisites) of transmitting data in XML format is being able to display it in virtually any manner with a simple change of XSL (because all of the data is already there).
Okay, so now I’ve set out to do the transformation server-side. Great. Not that big a deal. The one sticking point was that I needed to include the following line (which took some research):
That and a couple other changes to my PHP script, and everything was being delivered as (X)HTML to the browser. Even works on my cell phone. Great, score one for me!
A while goes by and I decide to change the SOAP call to get 12 hour data as opposed to 24 hour data. That would help to narrow down when rain, for example, was to be expected. This turned out to be a lot more work once I jumped back into the XSL(T). You see, the data is presented in 12 hour blocks, sequentially, in one section, day – night – day – night, etc., and grouped in another, high temperatures (group one) and low temperatures (group two). While this may not have presented a problem to someone more adept with XSL, it was a stumbling block for me, to say the least. After beating my head against the wall for some time, not sure how much exactly, I did get it to work … mostly. And to be sure, had I not decided it simply was not worth my effort, I would have hammered out the remaining issues. However, it suddenly dawned on me, to heck with XML, convert it to an array, and then I can do whatever I want in PHP.
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
Further research and several failed attempts (read that as code did not work) and I happened across a PHP class at: XML To Array With PHP which works. Include the class and add the following, and I was off to the races.
$weatherArr = new simpleXml2Array($weatherXML, null);
$arr = $weatherArr->arr['data']['parameters']['content'];
$arr now held everything I needed and could be dealt with in a straight forward fashion. Well, to be fair, straight forward to me.
tldr; XML is overly complicated and is not as useful as was originally intended (my opinion). XSLT should be done server-side. Better yet, convert XML to JSON or an array (for PHP consumption) and life will be much easier.