<?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>Jappit.com &#187; xml</title>
	<atom:link href="http://www.jappit.com/blog/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jappit.com/blog</link>
	<description>Mobile blog by Alessandro La Rosa</description>
	<lastBuildDate>Wed, 23 Nov 2011 10:38:23 +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>How to parse a generic XML file in J2ME with kXML</title>
		<link>http://www.jappit.com/blog/2008/05/15/how-to-parse-a-generic-xml-file-in-j2me-with-kxml/</link>
		<comments>http://www.jappit.com/blog/2008/05/15/how-to-parse-a-generic-xml-file-in-j2me-with-kxml/#comments</comments>
		<pubDate>Thu, 15 May 2008 09:48:49 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[kxml]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml parsing]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=38</guid>
		<description><![CDATA[Some days ago I&#8217;ve posted J2ME code to parse RSS feeds using kXML library. Today&#8217;s code is about parsing a generic XML, so you can use it to parse any XML document you want. Code is splitted in the following two classes. XmlNode class This is the class representing a single node. We&#8217;ll define 2 [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago I&#8217;ve posted <a title="Parsing RSS feeds with J2ME and kXML" href="http://www.jappit.com/blog/2008/05/09/parsing-rss-feeds-with-j2me-and-kxml/">J2ME code to parse RSS feeds</a> using <a title="kXML website" href="http://kxml.sourceforge.net/">kXML </a>library. Today&#8217;s code is about parsing a generic XML, so you can use it to parse any XML document you want. Code is splitted in the following two classes.</p>
<h3>XmlNode class</h3>
<p>This is the class representing a single node. We&#8217;ll define 2 node types:</p>
<ul>
<li><strong>Text nodes</strong>: nodes without a name, contanining only a textual value. For them we&#8217;ll define a TEXT_NODE final variable</li>
<li><strong>Element nodes</strong>: nodes with a tag name, that can have children nodes and/or attributes</li>
</ul>
<p>XmlNode class source code is available here: <a title="XmlNode.java source code" href="http://www.jappit.com/uploads/src/XmlNode.java">XmlNode.java</a></p>
<h3>GenericXmlParser class</h3>
<p>This is the class that will actually do XML parsing. We&#8217;ll define only one public method, parseXML(), that will accept 2 arguments:</p>
<ul>
<li>a <strong>KXmlParser </strong>instance, that will be used to do XML parsing</li>
<li>a <strong>boolean </strong>that will tell if whitespace-only text nodes must be ignored or not</li>
</ul>
<p>and will return and <strong>XmlNode </strong>representing the root node of the parsed XML tree.</p>
<p>GenericXmlParser class source code is available here: <a title="GenericXmlParser.java source code" href="http://www.jappit.com/uploads/src/GenericXmlParser.java">GenericXmlParser.java</a></p>
<h3>Sample usage</h3>
<p>Let&#8217;s see how we can use the two classes above. First we must instantiate and initialize a KXmlParser, and then we can call our GenericXmlParser parseXML() method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">InputStreamReader</span> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/test3.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
KXmlParser parser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> KXmlParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
parser.<span style="color: #006633;">setInput</span><span style="color: #009900;">&#40;</span>reader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
GenericXmlParser gParser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GenericXmlParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
XmlNode xml <span style="color: #339933;">=</span> gParser.<span style="color: #006633;">parseXML</span><span style="color: #009900;">&#40;</span>parser, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, we have our resulting XmlNode  that will hold the whole XML tree. We can make a simple dump of it to check if it&#8217;s all ok, using this test function:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> dumpXML<span style="color: #009900;">&#40;</span>XmlNode node, <span style="color: #000066; font-weight: bold;">int</span> deep<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> deep<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>node.<span style="color: #006633;">nodeName</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; - &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>node.<span style="color: #006633;">nodeValue</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(&quot;</span> <span style="color: #339933;">+</span> node.<span style="color: #006633;">nodeValue</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) - &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> attributes <span style="color: #339933;">=</span> node.<span style="color: #006633;">getAttributeNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> attributes.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> node.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>attributes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> node.<span style="color: #006633;">children</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		dumpXML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>XmlNode<span style="color: #009900;">&#41;</span>node.<span style="color: #006633;">children</span>.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>, deep <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><em>Note: This article is available also on Forum Nokia Wiki: <a title="parsing XML in J2ME Wiki article" href="http://wiki.forum.nokia.com/index.php/How_to_parse_an_XML_file_in_J2ME_with_kXML" target="_blank">How to parse an XML file in J2ME with kXML</a></em></p>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/05/15/how-to-parse-a-generic-xml-file-in-j2me-with-kxml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing RSS feeds with J2ME and KXML</title>
		<link>http://www.jappit.com/blog/2008/05/09/parsing-rss-feeds-with-j2me-and-kxml/</link>
		<comments>http://www.jappit.com/blog/2008/05/09/parsing-rss-feeds-with-j2me-and-kxml/#comments</comments>
		<pubDate>Fri, 09 May 2008 12:28:05 +0000</pubDate>
		<dc:creator>pit</dc:creator>
				<category><![CDATA[j2me]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[forumnokia]]></category>
		<category><![CDATA[kxml]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.jappit.com/blog/?p=33</guid>
		<description><![CDATA[Some days have passed since last J2ME tutorial, so here is a fresh new one! Today we&#8217;ll see how parsing a RSS feed with J2me is easy using KXML library, a fast and small XML pull parser, expecially suited for constrained environments like mobile devices. A live sample, parsing the RSS feed of this blog, [...]]]></description>
			<content:encoded><![CDATA[<p>Some days have passed since last J2ME tutorial, so here is a fresh new one!</p>
<p>Today we&#8217;ll see how parsing a RSS feed with J2me is easy using <a title="KXML library" href="http://kxml.sourceforge.net/" target="_blank">KXML </a>library, a fast and small XML pull parser, expecially suited for constrained environments like mobile devices. A live sample, parsing the RSS feed of this blog, is available <a title="J2ME KXML RSS parser emulator" href="http://www.jappit.com/index.php?page=emulator&amp;midlet=kxml_rss_parser">here</a>.</p>
<p><img src="http://www.jappit.com/images/blog/uploads/j2me_kxml_rss_parser.png" alt="J2ME Kxml rss parser screenshot" width="231" height="175" /></p>
<p>The detailed explanation of source code is available on my Forum Nokia Wiki article: <a title="J2ME RSS Parser with KXml" href="http://wiki.forum.nokia.com/index.php/J2ME_RSS_Parser_with_KXml" target="_blank">J2ME RSS Parser with KXml</a>. If you&#8217;re interested only in plain source code, you can pick it up <a title="J2ME kxml rss parser source code" href="http://www.jappit.com/uploads/src/kxmlrssparser.zip">here</a> (it includes also the sample midlet you find on the emulator page). To use KXmlRssParser class, you must simply do:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">KXmlRssParser parser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> KXmlRssParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">Vector</span> rssItems <span style="color: #339933;">=</span> parser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>yourFeedURL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and the parse() method will return the complete list of parsed Items, as instances of RssItem class. Source code is of course simplified, for the purpose of this tutorial, as it only considers title, link and description tags of each &lt;item&gt;, but once you understand KXml logic you can extend it, without much effort, to include other infos from RSS feed.</p>
<p>Other resources you might find useful:</p>
<ul>
<li>KXML JavaDocs: <a title="KXML JavaDocs" href="http://kxml.sourceforge.net/kxml2/javadoc/" target="_blank">http://kxml.sourceforge.net/kxml2/javadoc/</a></li>
<li>RSS 2.0 Specifications: <a title="RSS 2.0 specifications" href="http://cyber.law.harvard.edu/rss/rss.html" target="_blank">http://cyber.law.harvard.edu/rss/rss.html</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.jappit.com/blog/2008/05/09/parsing-rss-feeds-with-j2me-and-kxml/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

