Home  >  

Adobe AIR, Webkit and XML

Author photo
AddThis Social Bookmark Button

Normally, when working client-side within a web application and there's a need for data exchange, it feels very natural to use JSON as a data exchange format. It's easy to take a string from the server, convert it into a JavaScript object and then access what we need out of it.

Unencumbered by the demands of cross-browser support, Adobe AIR and Webkit allows for easy ways to manage data within the JavaScript environment.

Creating a new XML document

Creating a new document within the JavaScript environement can be very straightforward and you have a couple options depending on how you're retrieving the data.

If you're retrieving an XML document using the XMLHttpRequest object (as per a typical AJAX request) then the responseXML property should provide you with an XML object.

Alternatively, you can use the DOMParser object to parse a string into an XML object.

var parser=new DOMParser();
var xmldoc=parser.parseFromString(s,"text/xml");

With an XML document, you can use your favorite DOM methods to retrieve and interate through elements.

Converting an XML object into a string

Once you are done manipulating the XML object and you want to save the XML document somewhere, you need to serialize the XML into a string. That string can then be saved to the file system or sent to a remote server (for example, to do a SOAP call).

Serializing an XML document is done using the XMLSerializer:

var serializer = new XMLSerializer();
var packet = serializer.serializeToString(xmldoc);

Using XPath

Using the DOM methods can help you move around the XML document but there's a powerful way to be able to retrieve valuable information from your document: XPath. XPath is used to retrieve nodes and other information through an XPath expression. For example, let's say you were retrieving a list of photos from the Flickr API but only wanted to show those from your friends.

Here is an example API response:

<photos page="2" pages="89" perpage="10" total="881">
	<photo id="2636" owner="47058503995@N01" 
		secret="a123456" server="2" title="test_04"
		ispublic="1" isfriend="0" isfamily="0" />
	<photo id="2635" owner="47058503995@N01"
		secret="b123456" server="2" title="test_03"
		ispublic="0" isfriend="1" isfamily="1" />
	<photo id="2633" owner="47058503995@N01"
		secret="c123456" server="2" title="test_01"
		ispublic="1" isfriend="0" isfamily="0" />
	<photo id="2610" owner="12037949754@N01"
		secret="d123456" server="2" title="00_tall"
		ispublic="1" isfriend="0" isfamily="0" />
</photos>

To retrieve just your friend's photos, you could use the following XPath expression:

//photo[@isfriend=1]

This looks for any photo element that has an attribute of isfriend equal to 1. To execute the expression, it needs to be evaluated against the XML document:

xmldoc.evaluate("//photo[@isfriend=1]")

After the document is evaluated, a nodelist will be returned. From there, accessing the nodelist is a little different than using DOM methods. You'll need to use the iterateNext method to be able to move through the list.

while (thisNode = iterator.interateNext()) {
    alert( thisNode.textContent );
    thisNode = iterator.iterateNext();
  }	

XML over JSON?

Almost all REST APIs out there offer up XML as its primary format, making XML a very compelling option when working with data in the AIR environment. I still like the ability to work with JSON data in the browser but I think I'll be more inclined to use XML from now on in AIR.

Read more from Jonathan Snook. Jonathan Snook's Atom feed snookca on Twitter

Comments

2 Comments

nice post! it's good to see ajax developers pumping xml where it makes sense, i feel sometimes that json has a slightly religious following at times.

Nathan Mahon said:

isn't that last while loop skipping every other node?

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.