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


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

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.