<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>arjansnaterse.nl &#187; AJAX</title>
	<atom:link href="http://arjansnaterse.nl/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://arjansnaterse.nl</link>
	<description></description>
	<lastBuildDate>Wed, 02 Dec 2009 07:48:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AJAX, an object oriented XML parser</title>
		<link>http://arjansnaterse.nl/ajax-object-oriented-xml-parser</link>
		<comments>http://arjansnaterse.nl/ajax-object-oriented-xml-parser#comments</comments>
		<pubDate>Sun, 14 Sep 2008 19:07:51 +0000</pubDate>
		<dc:creator>Arjan</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://arjansnaterse.nl/?p=102</guid>
		<description><![CDATA[A lot of XML parsers that are used in AJAX scripts use a global variable to store the output. This works fine untill you want to want to do multiple requests at the same time. And that exactly should be one of the advantages of AJAX, the &#8216;a&#8217; of Asynchronous. So, what&#8217;s going wrong when [...]<p><a href="http://arjansnaterse.nl/ajax-object-oriented-xml-parser">AJAX, an object oriented XML parser</a> is a post from: <a href="http://arjansnaterse.nl">arjansnaterse.nl</a></p>
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Farjansnaterse.nl%2Fajax-object-oriented-xml-parser"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Farjansnaterse.nl%2Fajax-object-oriented-xml-parser&amp;style=normal&amp;service=bit.ly" height="61" width="50" title="AJAX, an object oriented XML parser" alt=" AJAX, an object oriented XML parser" /><br />
			</a>
		</div>
<p>A lot of XML parsers that are used in AJAX scripts use a global variable to store the output. This works fine untill you want to want to do multiple requests at the same time. And that exactly should be one of the advantages of AJAX, the &#8216;a&#8217; of Asynchronous.</p>
<p>So, what&#8217;s going wrong when you use a traditional parser? Let&#8217;s say you fire a request that takes two seconds to get the data you need. Now, within these 2 seconds you want to fire a new request. But the problem now is that for this request you need the same global variable as you used for the first request and the returned data of the first request will be be overwritten by the data of the second one. A solution would be to make a new global varainble for each request, but I don&#8217;t think that is quite a good and flexible solution. The best solution (in my opinion) is to make the parser object oriented. Now every request will get its own object and doesn&#8217;t interfere anymore with the other ones.</p>
<p>In this article I&#8217;ll give you the code and some explanation for this XML parser. I won&#8217;t give a full explanation of how object oriented programming works. You got to find it out yourself :-)</p>
<h2>The code</h2>
<p>The code itself is a little bit too much to display on this page, so you can <a href="http://arjansnaterse.nl/wp-content/uploads/2008/09/HTTPDataConnection.js">download it</a>. In the next paragraphs I&#8217;ll exaplain the most important parts.</p>
<h3>Initilization</h3>
<p>In line 9 to 16 a number of variables are declared which we will use later. After this declaration the init() function is called. This function is called every time a new object is created. You could use this to display a loading image or something like that. At the end of all, the _closeLoading is called where you can kill the loading image.</p>
<h3>Settings</h3>
<p>After the initialization there are a couple of functions which can be used to change the settings for the object. A short impression:</p>
<ul>
<li><code>setResponseAsText()</code>: default you will receive the data as XML. Calling this function will return the data as raw text.</li>
<li><code>setDisabledASync()</code>: This function will disable the asynchronous behaviour of the requests. By calling this function all request are handeld after each other.</li>
<li><code>setFunction()</code>: in <code>setFunction()</code> function you can mention all the functions (in comma separated format) that should be called when the data is ready.</li>
<li><code>setURL(url)</code>: define the URL of the server side script</li>
<li><code>addParam(param, value)</code>: in this function you can define which parameters should be added to the URL of the server side script. Call this function for each paramter.</li>
</ul>
<p>So this is what it look like to create a new request (<code>getResults</code> is a fictive function):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getResults<span style="color: #009900;">&#40;</span>query<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> xmlhttp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HTTPDataConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlhttp.<span style="color: #660066;">setURL</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;results.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlhttp.<span style="color: #660066;">addParam</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;query&quot;</span><span style="color: #339933;">,</span> query.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlhttp.<span style="color: #660066;">addParam</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;maxResults&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlhttp.<span style="color: #660066;">setFunction</span><span style="color: #009900;">&#40;</span>processData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlhttp.<span style="color: #660066;">requestData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The request</h3>
<p>The most important function is <code>requestData()</code>. In this function the actual XmlHTTPRequest is done. <code>handleData()</code> will process the obtained data.</p>
<h3>Data processing</h3>
<p>The <code>handleData()</code> function gets the data after the request is finished (<code>readystate</code> == 4). The received (XML) data is stored in the <code>this.receive</code> variabele.</p>
<p>Here&#8217;s a little example of an XML document:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">		<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[Seth Godin]]--&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[9789022994702]]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[Portfolio]]--&gt;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[Malcolm Gladwell]]--&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[9781586217457]]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[CDATA[Hachette Audio]]--&gt;</span></pre></div></div>

<p>After getting the data all the function from the <code>setFunction()</code> function are being called.At the end <code>_closeLoading()</code> is kicked wheere you can finalize your request a let your loading images disappear.</p>
<h3>Displaying the data</h3>
<p>If you to show the data on the screen you can do this in one of the functions that is been called after the request. In the example below this function is called <code>processInput()</code>. The comments are clear enough I think :-)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> processInput<span style="color: #009900;">&#40;</span>xmlhttp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// get data as an array, with XML element 'object' as root</span>
	<span style="color: #003366; font-weight: bold;">var</span> dataArray <span style="color: #339933;">=</span> xmlhttp.<span style="color: #660066;">getDataAsArray</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// get number of results from XML element 'count'.</span>
	<span style="color: #003366; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> xmlhttp.<span style="color: #660066;">getDataValue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">25</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			output <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;
&nbsp;
&lt;em&gt;There are more than 25 results. Please refine your search query.&lt;/em&gt;
&nbsp;
&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			output <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;
&nbsp;
&lt;em&gt;There are &quot;</span> <span style="color: #339933;">+</span> count <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; results found&lt;/em&gt;
&nbsp;
&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> object <span style="color: #000066; font-weight: bold;">in</span> dataArray<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>dataArray<span style="color: #009900;">&#91;</span>object<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> dataArray<span style="color: #009900;">&#91;</span>object<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> author <span style="color: #339933;">=</span> dataArray<span style="color: #009900;">&#91;</span>object<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;author&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> ean <span style="color: #339933;">=</span> dataArray<span style="color: #009900;">&#91;</span>object<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;ean&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> publisher <span style="color: #339933;">=</span> dataArray<span style="color: #009900;">&#91;</span>object<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;publisher&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;
&nbsp;
&quot;</span><span style="color: #339933;">;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&lt;strong&gt;&quot;</span> <span style="color: #339933;">+</span> title <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/strong&gt;, &quot;</span> <span style="color: #339933;">+</span> author <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;EAN: &quot;</span> <span style="color: #339933;">+</span> ean <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;Published by: &quot;</span> <span style="color: #339933;">+</span> publisher<span style="color: #339933;">;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;
&nbsp;
&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'output'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> output<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The end</h3>
<p>So this is the object oriented XML parser. I hope that this is usefull one for you. I you have questions or remarks just drop me a note.</p>
<p><a href="http://arjansnaterse.nl/ajax-object-oriented-xml-parser">AJAX, an object oriented XML parser</a> is a post from: <a href="http://arjansnaterse.nl">arjansnaterse.nl</a></p>
]]></content:encoded>
			<wfw:commentRss>http://arjansnaterse.nl/ajax-object-oriented-xml-parser/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
