<?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>Joe Lanman &#187; objects</title>
	<atom:link href="http://www.joelanman.com/by_tag/objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joelanman.com</link>
	<description>Web design and development when not playing computer games</description>
	<lastBuildDate>Sat, 18 Dec 2010 19:10:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using JQuery and Javascript to parse XML to objects</title>
		<link>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/</link>
		<comments>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 00:23:34 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=3</guid>
		<description><![CDATA[Right &#8211; thought I&#8217;d post some actual code as proof of actual development. I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be checked for dangerous syntax, and then eval-ed and it becomes [...]]]></description>
			<content:encoded><![CDATA[<p>Right &#8211; thought I&#8217;d post some actual code as proof of actual development.</p>
<p>I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be <a href="http://docs.mootools.net/Utilities/JSON" class="aga aga_3">checked for dangerous syntax</a>, and then eval-ed and it becomes Javascript objects ready to use. The <a href="http://www.youtube.com/watch?v=IE9ripDJHng&amp;feature=related" class="aga aga_4">pot-noodle</a> of data formats.</p>
<p><span id="more-3"></span></p>
<p>Pot noodles are all very well but my friend <a href="http://www.x13n.com/" class="aga aga_5">Alex</a> prefers XML. It&#8217;s much more readable, and pretty much every language and its dog can parse it. I wrote a function to turn my Python data into XML which I&#8217;ll post later, but this post is about my Javascript XML parser, as it was harder to get right.</p>
<p>In summary the parser function takes a complex heirarchical XML tree with nodes and attributes, and turns it into equivalent JS objects and properties. The full code as it stands is at the end of the post, I&#8217;ll go through the main steps here:</p>
<ol>
<li>The function accepts the first child of the XML file and a parent object as arguments.</li>
<li>If the node name ends with &#8216;_array&#8217; then it is, unsurprisingly, an array. This is a naming convention, but I couldn&#8217;t see how else to make sure arrays were consistently dealt with.</li>
<li>If the node has no attributes of its own, treat it as an attribute itself. Again, this is a weird convention on my part, so I can use nodes to store large sections of text, instead of putting them in XML attributes, which would be hard to read.</li>
<li>Check whether the object can be pushed into a parent array, if that fails the parent must be an object &#8211; append the the new object to the parent.</li>
<li>Loop the attributes and assign them as properties to the object.</li>
<li>Loop the child nodes and send them to the function with the current array/object as parent.</li>
</ol>
<p><strong>Javascript code:</strong></p>
<pre>
<code>
xmlToObj : function(xml,parent_obj) {

	if (parent_obj == null)
	{

		// base object

		var new_obj = {};
		xml = xml.firstChild;

	} else if (xml.nodeName.search(/_array$/) != -1) {

		// array

		var new_obj = parent_obj[xml.nodeName] = [];

	} else if(xml.attributes.length == 0) {

		// node which is really a string attribute

		try {

			parent_obj[xml.nodeName] = xml.firstChild.nodeValue;

		} catch(error) {

			parent_obj[xml.nodeName] = '';

		}

	} else {

		if (parent_obj instanceof Array) {

			//member of an array

			var new_obj = {};
			parent_obj[$(xml).attr('id')] = new_obj;

		} else {

			// new object

			var new_obj = parent_obj[xml.nodeName] = {};

		}				

	}

	// map xml attributes to object properties

	$(xml.attributes).each(function()
	{
		if (this.nodeValue.toLowerCase() == 'true') {

			new_obj[this.nodeName] = true;

		} else if (this.nodeValue.toLowerCase() == 'false') {

			new_obj[this.nodeName] = false;

		} else {

			new_obj[this.nodeName] = parseInt(this.nodeValue);

		}
	});

	if ($(xml).children().length &gt; 0)
	{

		$(xml).children().each(function()
		{
			page_edit.xmlToObj(this, new_obj);

		});

	}

	return new_obj;

}</code></pre>
<p><strong>XML data</strong></p>
<pre>
<code>
&lt;page active="True" id="1" is_template="False" position="1" template_id="1" visible="True"&gt;
	&lt;title&gt;
		project
	&lt;/title&gt;
	&lt;sections_array&gt;
		&lt;page_section id="1" page_id="1" position="0" section_id="1" visible="True"&gt;
			&lt;section active="True" attribs="None" id="1"&gt;
				&lt;elements_array&gt;
					&lt;section_element element_id="1" id="1" position="0" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" id="1"&gt;
							&lt;content&gt;

								test
							&lt;/content&gt;
							&lt;title&gt;
								title
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
					&lt;section_element element_id="2" id="2" position="1" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" content="None" id="2"&gt;
							&lt;title&gt;
								test
							&lt;/title&gt;

						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;
				&lt;title&gt;
					test_section
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
		&lt;page_section id="2" page_id="1" position="1" section_id="2" visible="True"&gt;
			&lt;section active="True" attribs="None" id="2"&gt;

				&lt;elements_array&gt;
					&lt;section_element element_id="4" id="4" position="0" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="4"&gt;
							&lt;content&gt;
								Hello my name is Joe
							&lt;/content&gt;
							&lt;title&gt;
								Fred
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;

					&lt;section_element element_id="3" id="3" position="1" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="3"&gt;
							&lt;content&gt;
								some content
							&lt;/content&gt;
							&lt;title&gt;
								Feedy doo da
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;

				&lt;title&gt;
					arab
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
	&lt;/sections_array&gt;
&lt;/page&gt;
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.638 seconds -->

