<?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>ADM Blog &#187; Java</title>
	<atom:link href="http://blog.another-d-mention.ro/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.another-d-mention.ro</link>
	<description>No matter how you see things, reality changes when you reach understanding</description>
	<lastBuildDate>Wed, 11 Aug 2010 08:07:27 +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>The simplest way to parse XML in Java</title>
		<link>http://blog.another-d-mention.ro/programming/the-simplest-way-to-parse-xml-in-java/</link>
		<comments>http://blog.another-d-mention.ro/programming/the-simplest-way-to-parse-xml-in-java/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 10:54:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.another-d-mention.ro/?p=569</guid>
		<description><![CDATA[You can do a lot with XML, but often all you want to do is to read a simple file with some basic data in it. The options for doing this, SAX, DOM and JAXB are all relatively verbose and often off-putting. So here is a class that will make all this much much more [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p><!-- adman -->You can do a lot with XML, but often all you want to do is to read a simple file with some basic data in it. The options for doing this, SAX, DOM and JAXB are all relatively verbose and often off-putting. So here is a class that will make all this much much more simple.</p>
<p>Parsing a file such as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;config<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>test<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;version</span> <span style="color: #000066;">major</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">minor</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;roles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;user&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/roles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;joe&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;pass&quot;</span> <span style="color: #000066;">role</span>=<span style="color: #ff0000;">&quot;admin&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;harry&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;secret&quot;</span> <span style="color: #000066;">role</span>=<span style="color: #ff0000;">&quot;user&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/users<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Can be achieved with the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Xml config <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.xml&quot;</span>,<span style="color: #0000ff;">&quot;config&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #0000ff;">&quot;title: &quot;</span><span style="color: #339933;">+</span>config.<span style="color: #006633;">child</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Xml version <span style="color: #339933;">=</span> config.<span style="color: #006633;">child</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<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: #0000ff;">&quot;version: &quot;</span><span style="color: #339933;">+</span>version.<span style="color: #006633;">integer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;major&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">+</span>version.<span style="color: #006633;">integer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;minor&quot;</span><span style="color: #009900;">&#41;</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>Xml role<span style="color: #339933;">:</span>config.<span style="color: #006633;">child</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;roles&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;role&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	<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: #0000ff;">&quot;role: name: &quot;</span><span style="color: #339933;">+</span>role.<span style="color: #006633;">string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</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>Xml user<span style="color: #339933;">:</span>config.<span style="color: #006633;">child</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">children</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</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;">println</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;user: name: &quot;</span><span style="color: #339933;">+</span>user.<span style="color: #006633;">string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>
		<span style="color: #0000ff;">&quot;, password: &quot;</span><span style="color: #339933;">+</span>user.<span style="color: #006633;">string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>
		<span style="color: #0000ff;">&quot;, role: &quot;</span><span style="color: #339933;">+</span>user.<span style="color: #006633;">string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;role&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, it's nice and simple and allows you to get to the information quickly and without any hassle. The API uses the DOM parser underneath, but attempts to make the data more easily available. All you need is the following [<a href="http://blog.another-d-mention.ro/stuff/Xml.java">Click to download</a>] class, which you can of course customise however you like.</p>
<p><strong>Update</strong>:<br />
I've just added another nice method to this class. Is called e4xEval, that is actualy a xpath eval but i made it use the flex e4x syntax. So, if you want to get all the role name you can do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
Xml config <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.xml&quot;</span>,<span style="color: #0000ff;">&quot;config&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> list <span style="color: #339933;">=</span> config.<span style="color: #006633;">e4xEval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.roles.role.@name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// @ is attribute</span></pre></td></tr></table></div>

<p><!-- adman --></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.another-d-mention.ro/programming/the-simplest-way-to-parse-xml-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
