Consuming Remote XML as JSONP

by Erik Runyon on January 11, 2011

It’s All About Sharing Content

One of the basic tenets behind the internet is sharing data. Initially, that was primarily done with HTML. But with the rise of web applications, sometimes plain HTML just won’t do. That’s where API’s and feeds come in. An application with a proper API allows other applications to query it for content and get that content back in a variety of formats, be it XML, JSON, or HTML. Feeds on the other hand are a one-way distribution of data, usually in XML format.

The Problem

Notre Dame’s mobile site (m.nd.edu) is built solely on remote data. Every module draws from an external feed of either XML or javascript or in the case of the Map module, an API. This isn’t a problem since m.nd.edu is built in PHP, which has a well-established ability to fetch remote data and manipulate it. I’m in the process of converting the m.nd.edu application into a native iPhone app using PhoneGap. However, PhoneGap-based applications cannot contain PHP code. As a result I’m converting each of those modules to javascript and HTML. Unfortunately, javascript suffers from one very big drawback when it comes to consuming data… it’s only allowed to access files that exist on the domain running the script. The only exception to this is JSONP.

The Solution

The majority of the external feeds utilized by m.nd.edu are XML/RSS, so this presents a problem when the app needs JSONP. The easiest solution is to make use of a proxy script which creates JSONP from a XML feed. Here’s the rough-draft:

 <?php header('content-type: application/json; charset=utf-8'); if( strlen($_GET["feed"]) >= 13 ) { $xml = file_get_contents(urldecode($_GET["feed"])); if($xml) { $data = @simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($data); echo isset($_GET["callback"]) ? "{$_GET[’callback’]}($json)" : $json; } } ?> 

So what we have here is fairly basic. First off, the script is set to respond with the content type of “application/json”. PHP gets the XML from the remote source, then converts it to a SimpleXML object. This object can then be converted to JSON using the json_encode() function which has been available since PHP 5.2.1. The script outputs plain JSON. If a callback function is specified, it will wrap the output in the callback. I wouldn’t recommend using this as-is in production as it’s lacking some error-checking, but it’s enough to get started. At this point, you can use it with jQuery like so:

 $.getJSON("http://yoursite.com/xml2json.php?callback=?", {feed:"http://agency.nd.edu/agencynd-team.xml"}, function(data) { // process data here } }); 

So there you have it. We can now consume remote XML data using javascript. If anyone more familiar with PhoneGap knows of an easier/better way of dealing with this kind of a problem, please share.

Update 2012-02-04

Ben Alman (@cowboy) has released an open-source solution:

RSI and the Magic Trackpad

by Erik Runyon on January 4, 2011

Repetitive Stress Injury (RSI) and I are long-time enemies. For me, it manifests as pain from the wrist of my right hand moving up to near the elbow. This started my senior year of college. Near the end of graduation I picked up my first ergonomic keyboard (an Adesso model) that followed me to my first post-college job as an audio engineer. Since those early days I’ve tried a half-dozen or so split keyboards and wide variety of ergonomic Logitech mice, all the while searching for that magical combination that would reduce or eliminate the aches and pains that is RSI.

I’ve always been a keyboard jockey, using shortcut keys and other tools to avoid going to my pointer device as much as possible. This includes turning on Keyboard Acccess in the Mac’s System Preferences and learning as many keyboard shortcuts as possible. A nice tool my colleague recently pointed out to me is MenuPop by Binary Bakery, which puts an apps menu in a contextual menu for easy keyboard access. It’s essentially the same as what’s built in to MacOS with control-f2, but it displays a mini menu next to the cursor. But even with all these other tools, a lot of time is still spent moving the ever-present pointer to accomplish one thing or another.

Magic Trackpad

Magic TrackpadShorty after Apple’s Magic Trackpad came out, I figured I’d give it a shot to see if it would make a difference. My hope was that with the variety of ways to interact with it, the work of controlling that blasted cursor could be shared by all the phalanges and not just Mr. Pointer, whom I believe is the main culprit of my RSI.

If you’ve used any of Apple’s recent laptops, then you’ll already be familiar with the gestures and use of the trackpad, which is essentially what it is — a detached 5″x5″ notebook trackpad. The device runs on two AA batteries, and the battery life is quite impressive. I’ve been using it as my primary device for three months now and it still has 50% life on the batteries that shipped with the unit. Compare that to recharging every-other day with my mouse.

Back to the question of RSI. After three months of use, I’ve noticed a significant decrease in pain. Notice: I’m not a doctor, and make no claims beyond personal experience. It does take a little getting used to, especially when working in apps such as Photoshop and Fireworks (click-drag in particular takes practice). I believe there are a couple changes in interaction that led to the pain reduction.

Wrist Movement

With a mouse, I’ve found I rest my wrist on either the desktop or a cushioned mouse-pad, and tend to move the mouse by twisting my wrist while leaving my forearm stationary. I know this is incorrect mousing, but when I’m not paying attention, it’s what I do. With the trackpad, I’m making use of more full-hand gestures with less motion at the wrist.

Clicking

With a mouse, almost all interaction is done with the dominate pointer finger. With the trackpad, and depending on your settings, primary clicks can be done by compressing the lower-left corner of the trackpad using any part of the hand. There is also the option of enabling touch-clicking, which allows a light tapping on any part of the trackpad from any finger to act as a click. This spreads the workload considerably. Likewise, right-click can be done by either compressing the lower-right of the device, or by a two-finger touch anywhere on the device. These changes in clicking habits has proven the most beneficial in my case.

Conclusion

I’ll admit, I do keep my trusty ol’ Logitech mouse nearby, but I’m using the trackpad 99% of the time. I still use my mouse occasionally to switch-up which motions I’m using to interact with the GUI. Also, I’m not much of a gamer, so I can’t speak to that aspect of it. For my daily use, it has definitely taken over as my primary pointer peripheral and I recommend that anyone with RSI at least give it a shot.