<?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>Sinceriously &#187; xml</title>
	<atom:link href="http://www.sinceriously.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sinceriously.com</link>
	<description>Designment and Development</description>
	<lastBuildDate>Sun, 18 Jul 2010 01:36:08 +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>Crossword Web App, Part 6</title>
		<link>http://www.sinceriously.com/2009/03/crossword-web-app-part-6/</link>
		<comments>http://www.sinceriously.com/2009/03/crossword-web-app-part-6/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 20:31:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Game Designment]]></category>
		<category><![CDATA[crossword]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.sinceriously.com/?p=217</guid>
		<description><![CDATA[Although it&#8217;s easy to get Javascript to read from an XML file, pulling out the appropriate data can sometimes be tricky. Here are some tips to help you tame the XML beast. Part 6: Taming XML Let&#8217;s look at a typical XMLHttpRequest again. var url = 'xml/070814.xml';var xmlhttp = new XMLHttpRequest();&#160;if (xmlhttp != null) {xmlhttp.onreadystatechange [...]]]></description>
			<content:encoded><![CDATA[<p>Although it&#8217;s easy to get Javascript to read from an XML file, pulling out the appropriate data can sometimes be tricky. Here are some tips to help you tame the XML beast.</p>
<h1>Part 6: Taming XML</h1>
<p></p>
<p>Let&#8217;s look at a typical <code>XMLHttpRequest</code> again.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var url = 'xml/070814.xml';</code><code class="code-breakout">var xmlhttp = new XMLHttpRequest();</code><code class="code-breakout">&nbsp;</code><code class="code-breakout">if (xmlhttp != null) {</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.onreadystatechange = stateChange;</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.open('GET', url, true);</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.send(null);</code><code class="code-breakout">} else {</code><code class="code-breakout" style="margin-left:18px;">alert('Your browser does not support XMLHTTP.'); }</code><code class="code-breakout">}</code><code class="code-breakout">&nbsp;</code><code class="code-breakout">function stateChange() {</code><code class="code-breakout" style="margin-left:18px;">if (xmlhttp.readyState == 4) {</code><code class="code-breakout" style="margin-left:36px;">if (xmlhttp.status == 200) {</code><code class="code-breakout" style="margin-left:54px;">/*</code><code class="code-breakout" style="margin-left:54px;">A status of 200 means that the XML file was</code><code class="code-breakout" style="margin-left:54px;">loaded and parsed without error. At this point</code><code class="code-breakout" style="margin-left:54px;">you'll want to sort through all of the data so</code><code class="code-breakout" style="margin-left:54px;">that it can be used by your program. I'll give</code><code class="code-breakout" style="margin-left:54px;">you some tips on how to do this in the next</code><code class="code-breakout" style="margin-left:54px;">update.</code><code class="code-breakout" style="margin-left:54px;">*/</code><code class="code-breakout" style="margin-left:36px;">} else {</code><code class="code-breakout" style="margin-left:54px;">alert('Problem retrieving XML data.');</code><code class="code-breakout" style="margin-left:36px;">}</code><code class="code-breakout" style="margin-left:18px;">}</code><code class="code-breakout">}</code></div>
<p><span>Figure 1. The basic structure of an XMLHttpRequest</span></div>
<p></p>
<p><span id="more-217"></span></p>
<p>If the request succeeds without error, then we can use the <code>xmlhttp</code> object to access the contents of the loaded XML file. To understand how to work with an <code>XMLHttpRequest</code> object, let&#8217;s relate it to something a little more familiar to Web coders: the Javascript <code>Document</code> object.</p>
<p><code>document.getElementsByName('across')</code> will return an array containing all of the elements on your Web page whose <code>name</code> <em>attribute</em> is &#8220;across.&#8221; For example, <code>&lt;div name="across"&gt;</code>.</p>
<p><code>xmlhttp.responseXML.getElementsByTagName('across')</code> will return an array containing all of the &#8220;across&#8221; XML nodes. For example, <code>&lt;across&gt;Data&lt;/across&gt;</code>.</p>
<p>Nodes can have both <em>attributes</em> and <em>values</em>. Review Figure 2.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code><code class="code-breakout">&lt;editor&gt;Timothy Parker&lt;/editor&gt;</code><code class="code-breakout">&lt;across&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a1 a="POET" c="Burns or Byron" n="1" cn="1" /&gt;</code><code class="code-breakout">&lt;/across&gt;</code></div>
<p><span>Figure 2. <strong>c</strong> is an attribute of the <strong>a1</strong> node.<br />&#8220;Timothy Parker&#8221; is the value of the <strong>editor</strong> node.</span></div>
<p></p>
<p><code>getElementsByTagName</code> will always return an array. In the example above there is only one <code>a1</code> node, therefore there&#8217;s no reason to keep an <em>array</em> of <code>a1</code> nodes. Instead, using the code below, we can save only the first returned node (since that&#8217;s all that exists).</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var a1Node = xmlhttp.responseXML.getElementsByTagName('a1')[0];</code></div>
<p><span>Figure 3. Save a reference to the <strong>a1</strong> XML node in a variable named <strong>a1Node</strong>.</span></div>
<p></p>
<p>Now that we&#8217;ve captured the <code>a1</code> node specifically, we can access its <code>c</code> <em>attribute</em> using this simple code: <code>var c = a1.getAttribute('c');</code>.</p>
<p>Accessing node <em>attributes</em> is easy, but accessing node <em>values</em> is a little more confusing. Figure 4 demonstrates how to access the <em>value</em> of the <code>editor</code> node.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var editor = xmlhttp.responseXML.getElementsByTagName('editor')[0];</code><code class="code-breakout">var editorValue = editor.firstChild.nodeValue;</code></div>
<p><span>Figure 4. Accessing the value of the <strong>editor</strong> node.</span></div>
<p></p>
<p>The <code>firstChild</code> method returns the first child of a node, duh. But the <code>editor</code> node does not appear to have any children. Well, it apparently (or not so apparently) does. When attempting to access the <em>value</em> of a node, imagine the value text as being a node itself. Access the <code>nodeValue</code> of the &#8220;text node&#8221; and you&#8217;ll get the result you&#8217;re looking for. Or, in more simple terms, always use <code>.firstChild.nodeValue</code> to access the value of a node.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code><code class="code-breakout">&lt;across&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a1 a="POET" c="Burns or Byron" n="1" cn="1" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a2 a="BLAB" c="Give everything away" n="6" cn="5" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a3 a="TALKS" c="Gives everything away" n="11" cn="9" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a4 a="ALSO" c="Too" n="16" cn="14" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a5 a="YOHO" c="Exclamation by Captain Jack Sparrow" n="21" cn="15" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a6 a="ELIOT" c="'A Cooking Egg' writer" n="26" cn="16" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a7 a="LIAR" c="Ananias, for one" n="31" cn="17" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a8 a="PREY" c="Target in the wild" n="36" cn="18" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a9 a="ALONE" c="In isolation" n="41" cn="19" /&gt;</code><code class="code-breakout">&lt;/across&gt;</code></div>
<p><span>Figure 5. Another XML example.</span></div>
<p></p>
<p>In the above example, all of the <code>across</code> child nodes are named differently, so it would be cumbersome to <code>getElementsByTagName</code> each one separately. Instead, take advantage of the <code>childNodes</code> collection to return an array of the nodes.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout"> var acrossNodes = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;</code></div>
<p><span>Figure 6. Save all of the <strong>across</strong> child nodes to an array.</span></div>
<p></p>
<p>There is more to learn about <code>XMLHttpRequest</code>, but by simply knowing how to use the <code>getElementsByTagName</code>, <code>getAttribute</code>, <code>firstChild</code> and <code>nodeValue</code> methods and the <code>childNodes</code> collection, you&#8217;ll have an easy time retrieving all types of data from XML documents.</p>
<p>Before I close this lengthy post, there is one more thing I&#8217;d like to mention with regards to XML files. It appears that you can not use Javascript to access local XML files using XMLHttpRequest, you can only access files found on a domain. I am completely perplexed as to why this is and if anyone  has knowledge of the reason, or knows a workaround, I&#8217;d love to hear from you.</p>
<p><strong>&mdash; Ryan</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://js-kit.com/rss/www.sinceriously.com/p=217</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
