<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23381-</id>
  <updated>2009-11-05T20:14:19Z</updated>
  <title>Comments for Adobe AIR, Webkit and XML (http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23381</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23381" title="Adobe AIR, Webkit and XML" />
    <published>2008-04-12T19:06:18Z</published>
    <updated>2009-01-06T16:08:59Z</updated>
    <title>Adobe AIR, Webkit and XML</title>
    <summary>Normally, when working client-side within a web application and there&apos;s a need for data exchange, it feels very natural to use JSON as a data exchange format. It&apos;s easy to take a string from the server, convert it into a JavaScript object and then access what we need out of it. But maybe there&apos;s a better way.</summary>
    <author>
      <name>Jonathan Snook</name>
      <uri>http://snook.ca/jonathan/</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<p>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.</p>
  <p>Unencumbered by the demands of cross-browser support, Adobe AIR and Webkit allows for easy ways to manage data within the JavaScript environment.</p>
  <h4>Creating a new XML document</h4>
  <p>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. </p>
  <p>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.</p>
  <p>Alternatively, you can use the DOMParser object to parse a string into an XML object. </p>
  
<pre>var parser=new DOMParser();<br />
var xmldoc=parser.parseFromString(s,&quot;text/xml&quot;);</pre>
    <p>With an XML document, you can use your favorite DOM methods to retrieve and interate through elements. </p>
    <h4>Converting an XML object into a string</h4>
    <p>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). </p>
    <p>Serializing an XML document is done using the XMLSerializer:</p>
    
<pre>var serializer = new XMLSerializer();<br />
var packet = serializer.serializeToString(xmldoc);</pre>
      <h4>Using XPath</h4>
      <p>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. </p>
      <p>Here is an example API response:</p>
<pre>&lt;photos page="2" pages="89" perpage="10" total="881"&gt;
	&lt;photo id="2636" owner="47058503995@N01" 
		secret="a123456" server="2" title="test_04"
		ispublic="1" isfriend="0" isfamily="0" /&gt;
	&lt;photo id="2635" owner="47058503995@N01"
		secret="b123456" server="2" title="test_03"
		ispublic="0" isfriend="1" isfamily="1" /&gt;
	&lt;photo id="2633" owner="47058503995@N01"
		secret="c123456" server="2" title="test_01"
		ispublic="1" isfriend="0" isfamily="0" /&gt;
	&lt;photo id="2610" owner="12037949754@N01"
		secret="d123456" server="2" title="00_tall"
		ispublic="1" isfriend="0" isfamily="0" /&gt;
&lt;/photos&gt;</pre>
<p>To retrieve just your friend's photos, you could use the following XPath expression:</p>

<pre>//photo[@isfriend=1]</pre>
<p>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:</p>
  
<pre>xmldoc.evaluate(&quot;//photo[@isfriend=1]&quot;)</pre>
<p>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.</p>
<pre>while (thisNode = iterator.interateNext()) {
    alert( thisNode.textContent );
    thisNode = iterator.iterateNext();
  }	</pre>
<h4>XML over JSON?</h4>
<p>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.</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23381-comment:2016603</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23381" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html#comment-2016603" />
    <title>Comment from Andre Charland on 2008-04-15</title>
    <author>
        <name>Andre Charland</name>
        <uri>http://blogs.nitobi.com/andre</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blogs.nitobi.com/andre">
        <![CDATA[<p>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.</p>]]>
    </content>
    <published>2008-04-16T00:06:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23381-comment:2042473</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23381" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/adobe-air-webkit-and-xml.html#comment-2042473" />
    <title>Comment from Nathan Mahon on 2008-09-09</title>
    <author>
        <name>Nathan Mahon</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>isn't that last while loop skipping every other node?</p>]]>
    </content>
    <published>2008-09-09T12:13:49Z</published>
  </entry>

</feed
