<?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>Arash Payan &#124; Blog &#187; apxml</title>
	<atom:link href="http://arashpayan.com/blog/tag/apxml/feed/" rel="self" type="application/rss+xml" />
	<link>http://arashpayan.com/blog</link>
	<description>(Place witty tagline here)</description>
	<lastBuildDate>Mon, 02 Apr 2012 03:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>APXML: NSXMLDocument &#8216;substitute&#8217; for iPhone/iPod Touch</title>
		<link>http://arashpayan.com/blog/2009/01/14/apxml-nsxmldocument-substitute-for-iphoneipod-touch/</link>
		<comments>http://arashpayan.com/blog/2009/01/14/apxml-nsxmldocument-substitute-for-iphoneipod-touch/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 05:04:45 +0000</pubDate>
		<dc:creator>arash</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[apxml]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://arashpayan.com/blog/?p=46</guid>
		<description><![CDATA[After spending some time working on Jabeh, my latest creation for iPhone/iPod Touch, I&#8217;m taking some time to dump a little learned knowledge into my blog. In my first app, my XML needs weren&#8217;t that great, so putting up with the lack of NSXMLDocument in the iPhone SDK was not a big deal. However, in [...]]]></description>
			<content:encoded><![CDATA[<p>After spending some time working on <a href="http://jabeh.org">Jabeh</a>, my latest creation for iPhone/iPod Touch, I&#8217;m taking some time to dump a little learned knowledge into my blog.</p>
<p>In my first app, my XML needs weren&#8217;t that great, so putting up with the lack of <code>NSXMLDocument</code> in the iPhone SDK was not a big deal. However, in <a href="http://jabeh.org">Jabeh</a> I was changing the XML format so often and using so much of it for my network communication creating delegates for <code>NSXMLParser</code> quickly became a huge time sink. After a little hacking, I came up with APXML to solve my DOM problem. It&#8217;s not a perfect implementation of the <a href="http://www.w3.org/TR/REC-xml/">W3C XML 1.0 standard</a>, but it&#8217;s close enough for a lot of usage. One particular shortcoming is its lack of support for namespaces but maybe somebody else can add that support. If you just want to jump in and start using it (LGPL license), you can get the code from github:</p>
<p><a href="http://github.com/arashpayan/apxml/">http://github.com/arashpayan/apxml/</a></p>
<p>Most of my XML manipulation experience has been with various Java libraries (<a href="http://java.sun.com/javase/6/docs/api/org/w3c/dom/package-summary.html">org.w3c.dom interface</a>, <a href="http://jdom.org/">JDOM</a> and <a href="http://xom.nu/">XOM</a>), and the only one that I enjoyed using was XOM, because of its simplicity and licensing. Almost all of my design decisions were based on how XOM does things.</p>
<p>Let&#8217;s say we want to represent the following XML document in memory using APXML:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881280;">&lt;books&gt;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#881280;">&lt;book </span><span style="color:#994500;">id</span><span style="color:#881280;">=</span><span style="color:#1a1aa6;">&quot;1&quot;</span><span style="color:#881280;"> </span><span style="color:#994500;">author</span><span style="color:#881280;">=</span><span style="color:#1a1aa6;">&quot;Michael Pollan&quot;</span><span style="color:#881280;">&gt;</span>The Omnivore&#8217;s Dilemma<span style="color:#881280;">&lt;/book&gt;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#881280;">&lt;book </span><span style="color:#994500;">id</span><span style="color:#881280;">=</span><span style="color:#1a1aa6;">&quot;2&quot;</span><span style="color:#881280;"> </span><span style="color:#994500;">author</span><span style="color:#881280;">=</span><span style="color:#1a1aa6;">&quot;Foley, van Dam, Feiner, Hughes&quot;</span><span style="color:#881280;">&gt;</span>Computer Graphics: Principles and Practices<span style="color:#881280;">&lt;/book&gt;</span><br />
<span style="color:#881280;">&lt;/books&gt;</span></div>
<p>In code, we do the following:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#683821;">#import &quot;APXML.h&quot;<br />
</span><br />
<span style="color:#881350;">@implementation</span> AppDelegate</p>
<p>- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">applicationDidFinishLaunching:</span>(UIApplication *)application {<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// create the document with it&#8217;s root element<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;APDocument *doc = [[APDocument <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">initWithRootElement:</span>[APElement <span style="color:#6c0540;">elementWithName:</span><span style="color:#760f15;">@&quot;books&quot;</span>]];<br />
&nbsp;&nbsp;&nbsp;&nbsp;APElement *rootElement = [doc <span style="color:#6c0540;">rootElement</span>]; <span style="color:#236e25;">// retrieves same element we created the line above<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// create the first book entry (The Omnivore&#8217;s Dilemma)<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;APElement *book1 = [APElement <span style="color:#6c0540;">elementWithName:</span><span style="color:#760f15;">@&quot;book&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[book1 <span style="color:#6c0540;">addAttributeNamed:</span><span style="color:#760f15;">@&quot;id&quot;</span> <span style="color:#6c0540;">withValue:</span><span style="color:#760f15;">@&quot;1&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[book1 <span style="color:#6c0540;">addAttributeNamed:</span><span style="color:#760f15;">@&quot;author&quot;</span> <span style="color:#6c0540;">withValue:</span><span style="color:#760f15;">@&quot;Michael Pollan&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[book1 <span style="color:#6c0540;">appendValue:</span><span style="color:#760f15;">@&quot;The Omnivore's Dilemma&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[rootElement <span style="color:#6c0540;">addChild:</span>book1];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// create the second book entry (Computer Graphics)<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;APElement *book2 = [APElement <span style="color:#6c0540;">elementWithName:</span><span style="color:#760f15;">@&quot;book&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[book2 <span style="color:#6c0540;">addAttributeNamed:</span><span style="color:#760f15;">@&quot;id&quot;</span> <span style="color:#6c0540;">withValue:</span><span style="color:#760f15;">@&quot;2&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[book2 <span style="color:#6c0540;">addAttributeNamed:</span><span style="color:#760f15;">@&quot;author&quot;</span> <span style="color:#6c0540;">withValue:</span><span style="color:#760f15;">@&quot;Foley, van Dam, Feiner, Hughes&quot;</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[rootElement <span style="color:#6c0540;">addChild:</span>book2];<br />
}</p>
<p><span style="color:#881350;">@end</span></div>
<p>And if we want to convert the document to an <code>NSString*</code>, we use one of the two methods in <code>APDocument</code>:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// converts the xml to a compact string with no newlines or tabs (good for production)<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSString</span> *xml = [doc <span style="color:#6c0540;">xml</span>];</div>
<p>or</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// converts the xml to an easy to read string with newlines and tabs (good for debugging)<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSString</span> *prettyXML = [doc <span style="color:#6c0540;">prettyXML</span>];</div>
<p>Often times when I&#8217;m working with XML, I like to see what the current element contains, so for added convenience, you can obtain an XML string containing the element you&#8217;re working with, its attributes and all its children directly from the <code>APElement</code> by calling one of two methods:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">- (<span style="color:#400080;">NSString</span>*)<span style="color:#6c0540;">prettyXML:</span>(<span style="color:#881350;">int</span>)tabs;<br />
- (<span style="color:#400080;">NSString</span>*)<span style="color:#6c0540;">xml</span>;</div>
<p>Now for the best part of the library, which is the ability to read in XML and represent it in APXML. All you have to execute is one simple line:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">&nbsp;&nbsp;&nbsp;&nbsp;APDocument *doc = [APDocument <span style="color:#6c0540;">documentWithXMLString:</span>xmlString];</div>
<p>Hopefully this will be helpful to other developers out there. I may post another article soon if anybody has some questions.</p>
<p><strong>UPDATE Sep 5, 2009:</strong> Here&#8217;s an example that demonstrates traversing the XML document.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">&nbsp;&nbsp;&nbsp;&nbsp;APElement *rootElement = [doc <span style="color:#6c0540;">rootElement</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSLog</span>(<span style="color:#760f15;">@&quot;Root Element Name: %@&quot;</span>, rootElement.name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// get all the child elements (each book)<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSArray</span> *childElements = [rootElement <span style="color:#6c0540;">childElements</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(APElement *child in childElements)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// returns the tag name<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSLog</span>(<span style="color:#760f15;">@&quot;Child Name: %@&quot;</span>, child.name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// reads the attribute named &#8216;author&#8217;<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSLog</span>(<span style="color:#760f15;">@&quot;Author: %@&quot;</span>, [child <span style="color:#6c0540;">valueForAttributeNamed:</span><span style="color:#760f15;">@&quot;author&quot;</span>]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// returns the text content of the element<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#400080;">NSLog</span>(<span style="color:#760f15;">@&quot;Title: %@&quot;</span>, [child <span style="color:#6c0540;">value</span>]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</div>
<p>In the console you&#8217;ll see (I&#8217;ve removed the <code>NSLog</code> markup):</p>
<div style="overflow:auto;">
<pre>
Root Element Name: books
Child Name: book
Author: Michael Pollan
Title: The Omnivore’s Dilemma
Child Name: book
Author: Foley, van Dam, Feiner, Hughes
Title: Computer Graphics: Principles and Practices
</pre>
</div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Farashpayan.com%2Fblog%2F2009%2F01%2F14%2Fapxml-nsxmldocument-substitute-for-iphoneipod-touch%2F&amp;title=APXML%3A%20NSXMLDocument%20%E2%80%98substitute%E2%80%99%20for%20iPhone%2FiPod%20Touch" id="wpa2a_2"><img src="http://arashpayan.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://arashpayan.com/blog/2009/01/14/apxml-nsxmldocument-substitute-for-iphoneipod-touch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

