<?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>Think2Loud &#187; XML</title> <atom:link href="http://think2loud.com/tag/xml/feed/" rel="self" type="application/rss+xml" /><link>http://think2loud.com</link> <description>Random thoughts from a few geeks</description> <lastBuildDate>Wed, 04 Jan 2012 01:43:02 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>JSON vs. XML: What Should You Use?</title><link>http://think2loud.com/680-json-xml/</link> <comments>http://think2loud.com/680-json-xml/#comments</comments> <pubDate>Sun, 27 Dec 2009 21:02:54 +0000</pubDate> <dc:creator>Jared</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[json]]></category> <category><![CDATA[XML]]></category><guid isPermaLink="false">http://think2loud.com/?p=680</guid> <description><![CDATA[<p>Lots of people have asked me this question: which is better when loading data via an ajax request?  JSON is my favorite. I'm going to try and explain the benefits of JSON over XML. <a href="http://think2loud.com/680-json-xml/">Read More</a></p><p><a href="http://think2loud.com/680-json-xml/">JSON vs. XML: What Should You Use?</a></p>]]></description> <content:encoded><![CDATA[<p>From the start, JSON is already has a leg up on XML for one reason: it&#8217;s faster. To read XML you need to parse it, read the nodes, attributes, and child nodes in the XML document, and then use the data that you&#8217;ve found.</p><p>In jQuery the easiest way to do that is to use the built-in ajax functions to call the XML and use the each function to loop through all the nodes. The each function is nice, but is much much slower than a for loop. Just imagine what happens when we have an XML document with multiple levels of nested nodes you need to get at. You end up with multiple nested $.each functions and without much trying, you end up with a script that crawls. With JSON it&#8217;s easy to get at the data since its already native javascript. No parsers or proxies necessary&#8211;all you need to do is loop through the data, fast and simple. That&#8217;s one point for JSON.</p><p>There are aspects of XML that people point to as reasons to use it.  First, XML is both human and machine readable. It is nice to see what data is being returned just by viewing the source, but that problem is solved by having good documentation, including an example of the JSON that will be returned. Also, being a front-end developer, I spend a lot of my time reading javascript anyway.  I find it just as easy to read as XML, it&#8217;s just different.</p><p>Another good reason to use XML is that it&#8217;s widely available.  Lots of websites offer APIs to get at their public (and sometimes private) data, and I usually find the returned format of these APIs are often either XML or JSON.  However since XML has been around longer than JSON, there is simply more XML out there.</p><p>Sometimes you might find yourself in a situation where you don&#8217;t have JSON as an option.  So do you submit to the API gods and slow down your site and just use the XML?  Why do that when you can write the API yourself!  This is where some server side skills come in handy.  It&#8217;s super easy to load an XML document via PHP and print out the data in whatever format you need it to be in.</p><p>So in conclusion, you should be using JSON every chance you get.</p><p><a href="http://think2loud.com/680-json-xml/">JSON vs. XML: What Should You Use?</a></p>]]></content:encoded> <wfw:commentRss>http://think2loud.com/680-json-xml/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Using jQuery and XML to Populate a Drop-Down Box</title><link>http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/</link> <comments>http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/#comments</comments> <pubDate>Tue, 10 Mar 2009 00:30:41 +0000</pubDate> <dc:creator>Josh</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[XML]]></category><guid isPermaLink="false">http://think2loud.com/?p=288</guid> <description><![CDATA[<p>Building on the post <a href="/reading-xml-with-jquery/">Reading XML with jQuery</a>, I have modified the success function to parse the XML data and load it into a drop-down box. You can get the demo files from Reading XML with jQuery <a href="/examples/XML.zip">here</a>. Once you download and unzip the demo files, open the index.html in your favorite editor. Then remove all the HTML between the body tag and also all the jQuery in the success function. I also changed the page title to "Using jQuery and XML to Populate a Drop-Down Box Demo." <a href="http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/">Read More</a></p><p><a href="http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/">Using jQuery and XML to Populate a Drop-Down Box</a></p>]]></description> <content:encoded><![CDATA[<p><a target="_blank" href="/examples/jquery-dropdown-xml/jquery-dropdown-xml.zip" class="button">Source</a> <a target="_blank" href="/examples/jquery-dropdown-xml/" class="button">Demo</a> <a target="_blank" class="button" href="/examples/XML.zip">Reading XML with jQuery Source</a></p><p><span id="more-288"></span></p><p>You&#8217;ll now have an index.html file that looks like this:</p><div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;all&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Using jQuery and XML to populate a drop-down box demo<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
     	$(document).ready(function(){
			$.ajax({
				type: &quot;GET&quot;,
				url: &quot;sites.xml&quot;,
				dataType: &quot;xml&quot;,
				success: function(xml) {
&nbsp;
				}
			});
		});
     <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;page-wrap&quot;</span>&gt;</span>
    	<span style="color: #808080; font-style: italic;">&lt;!-- all tags removed --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div><p>Next you&#8217;ll need a simple XML file to load.</p><p>Here is the one that I used : <a target="_blank" href="/examples/jquery-dropdown-xml/dropdown.xml">dropdown.xml</a></p><div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dropdown<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>first set<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>second set<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>third set<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option 1a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>option 2b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/values<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dropdown<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Now that you&#8217;ve created your XML, add a form with a select box to the HTML page. I simply replaced everything within the body tag from our index.html from the previous article.</p><div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mySelect&quot;</span>&gt;</span>
              <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span>&gt;</span>loading<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div><p>Once the XML is ready, jump back into the index.html file. You&#8217;ll need to update the jQuery Ajax call to get the new XML document. To do this, simply change the url to your new document.</p><p>old:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sites.xml&quot;</span><span style="color: #339933;">,</span>
	dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span>
	success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>new:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dropdown.xml&quot;</span><span style="color: #339933;">,</span>
	dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span>
	success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>With the Ajax call in place, you need to tell it what to do when it loads the XML in the success function. Start by getting the select tag with jQuery and storing it in a variable for later use. The select tag you are looking for has an id of mySelect.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> select <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mySelect'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Next, loop through all of the menuitem nodes in the XML with a jQuery each function.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'menuitem'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Inside the each function, you will get the title of your menuitem. Create an option tag as a header for the menuitem, and then create another jQuery each function to get all the possible values that go with the current menuitem.</p><p>The first thing you&#8217;ll want to do within the each function for the menuitem is get the title and append it to your select.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
select.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option/&gt;&lt;option class='ddheader'&gt;&quot;</span><span style="color: #339933;">+</span>title<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>There is a empty option tag in the code to get a space above the header, that way the drop down will be easier for the user to read. Add a class of ddheader to the option tag so in browser like firefox you can make the header bold.</p><p>With the header now inserted into the select, you&#8217;ll now need to create the options that will be under that header. To do this, use another jQuery each function, looking for all the value nodes that are in the menuitem node. Within this each you will get the text in the value node and then append it to the select tag as an option with a class of ddindent. The ddindent class is used to push these items to the right a bit so they are easy to follow.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	select.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;option class='ddindent' value='&quot;</span><span style="color: #339933;">+</span> value <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&gt;&quot;</span><span style="color: #339933;">+</span>value<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&lt;/option&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>That is just about everything, there&#8217;s only one more thing. At the end of the success function, change the option tag you hard coded in the index.html file that said &#8220;loading&#8221; to now say &#8220;please make a selection.&#8221; Make sure that it&#8217;s selected to start with by giving it the selected attribute. Because the option tag is the first child of the select tag, you can use the children function with the optional parameter of &#8220;:first&#8221;. This will return only the first child of the select tag.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">select.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;please make a selection&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>You should now have a functional drop-down box loaded with XML via jQuery.</p><p><a target="_blank" href="/examples/jquery-dropdown-xml/jquery-dropdown-xml.zip" class="button">Source</a> <a target="_blank" class="button" href="/examples/jquery-dropdown-xml/">Demo</a> <a target="_blank" href="/examples/XML.zip" class="button">Reading XML with jQuery Source</a></p><p><a href="http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/">Using jQuery and XML to Populate a Drop-Down Box</a></p>]]></content:encoded> <wfw:commentRss>http://think2loud.com/288-using-jquery-and-xml-to-populate-a-drop-down-box/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>Reading XML with jQuery</title><link>http://think2loud.com/224-reading-xml-with-jquery/</link> <comments>http://think2loud.com/224-reading-xml-with-jquery/#comments</comments> <pubDate>Sat, 06 Sep 2008 17:00:00 +0000</pubDate> <dc:creator>Jared</dc:creator> <category><![CDATA[JavaScript]]></category> <category><![CDATA[XML]]></category><guid isPermaLink="false">http://www.think2loud.com/?p=224</guid> <description><![CDATA[<p>Welcome to my first of many articles for Think2Loud! Today I thought I'd start with something simple that will lead into several other topics. So here we go, head first into reading XML with <a href="http://jquery.com/" >jQuery</a>. <a href="http://think2loud.com/224-reading-xml-with-jquery/">Read More</a></p><p><a href="http://think2loud.com/224-reading-xml-with-jquery/">Reading XML with jQuery</a></p>]]></description> <content:encoded><![CDATA[<p>The first step is making sure you have jQuery included on your page.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div><p>Second, you should check the XML file you are going to read and make sure it&#8217;s free of errors. If you are unfamiliar with XML synatax, check out the W3School&#8217;s <a target="_blank" href="http://www.w3schools.com/Xml/xml_syntax.asp" >rules on XML syntax</a>. The file I&#8217;m using for this example is just a listing of some Web sites I enjoy.</p><p>Finally, you just need to tell jQuery to read the file and print out the contents. Start by adding the document.ready function to the page.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p><span id="more-224"></span><br /> Within the document.ready we start a jQuery ajax request to read the XML file. The ajax request takes four parameters: file type, url, dataType, and success. You&#8217;ll see below how these are set to read a file. The important thing to notice is the success parameter. We set it to a function that takes the data the request gets from the XML file.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sites.xml&quot;</span><span style="color: #339933;">,</span>
	dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span>
	success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>This is where the fun part comes in. Now that we are reading the XML, we need to find the data written within it and do something with it. We start by reading the returned data and using the find method to get all the nodes that match the text we supply (in this case, &#8220;site&#8221;), and then use the each function to loop through what the find function give to us.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'site'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>All that&#8217;s left is to get the data from that node and print it out. Do this by using the attr function, and the find function to get the text and any attributes on the nodes.</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;items&quot; id=&quot;link_'</span><span style="color: #339933;">+</span>id<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a href=&quot;'</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;'</span><span style="color: #339933;">+</span>title<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page-wrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Your final code should look something like this:</p><div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sites.xml&quot;</span><span style="color: #339933;">,</span>
		dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span>
		success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'site'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;items&quot; id=&quot;link_'</span><span style="color: #339933;">+</span>id<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a href=&quot;'</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;'</span><span style="color: #339933;">+</span>title<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page-wrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'desc'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> brief <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'brief'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> long <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'long'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;brief&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>brief<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#link_'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;long&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>long<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#link_'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>That&#8217;s all it takes to read XML with jQuery. Feel free to check out the <a target="_blank" href="http://www.think2loud.com/examples/xml/" >demo</a> and download the <a target="_blank" href="http://www.think2loud.com/examples/XML.zip">source code</a>.</p><p><a href="http://think2loud.com/224-reading-xml-with-jquery/">Reading XML with jQuery</a></p>]]></content:encoded> <wfw:commentRss>http://think2loud.com/224-reading-xml-with-jquery/feed/</wfw:commentRss> <slash:comments>45</slash:comments> </item> </channel> </rss>
<!-- Served from: think2loud.com @ 2012-02-03 21:48:10 by W3 Total Cache -->
