<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"
>

<channel>
	<title>acidum.de &#187; XML</title>
	<atom:link href="http://www.acidum.de/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.acidum.de</link>
	<description></description>
	<lastBuildDate>Sun, 08 Nov 2009 20:12:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use Groovy XML Parser with namespaces</title>
		<link>http://www.acidum.de/2008/04/01/how-to-use-groovy-xml-parser-with-namespaces/</link>
		<comments>http://www.acidum.de/2008/04/01/how-to-use-groovy-xml-parser-with-namespaces/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 09:10:31 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=75</guid>
		<description><![CDATA[The Groovy documentation gives the first hints about how to use the Groovy XML Parser. This post describes how to read an XML file into a groovy class instance. To check for a valid input document advanced XML programs require XSDs for a formal description. Of course the XML processing task becomes more difficult with [...]]]></description>
			<content:encoded><![CDATA[<p>The <a title="Groovy XML Processing" href="http://groovy.codehaus.org/Processing+XML">Groovy documentation</a> gives the first hints about how to use the Groovy XML Parser. This post describes how to read an XML file into a groovy class instance. To check for a valid input document advanced XML programs require XSDs for a formal description. Of course the XML processing task becomes more difficult with XML namespaces but the following expample shows how easy XML processing is with Groovy.</p>
<p>This demo illustrates how to read an XML file into Groovy objects. As the background I used the student lecture management. It stores students, assigned lectures with results and specific details about the lecture itself. The students are stored within a <strong>studenten</strong> node which itself contains a node for every student. A student may have a name, birthday and its assignments.</p>
<p>The second major node is the <strong>lehrveranstaltungen</strong> node that contains details about the lectures. The student assignments and the lectures are linked via identifier.</p>
<p>The following XML file is used as xml input:<br />
<code><br />
&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />
&lt;studentenverwaltung<br />
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />
xsi:schemaLocation="studentenverwaltung.xsd"<br />
xmlns="http://www.hpi.uni-potsdam.de/studentenverwaltung"&gt;<br />
&lt;studenten&gt;<br />
&lt;student geburtstag="19821221" matrikelnummer="123456"&gt;<br />
&lt;vorname&gt;John&lt;/vorname&gt;<br />
&lt;nachname&gt;Doe&lt;/nachname&gt;<br />
&lt;belegungen&gt;<br />
&lt;belegung lehrveranstaltung="_12" note="1.0"/&gt;<br />
&lt;/belegungen&gt;<br />
&lt;/student&gt;<br />
&lt;student geburtstag="19821221" matrikelnummer="654321"&gt;<br />
&lt;vorname&gt;Alice&lt;/vorname&gt;<br />
&lt;nachname&gt;Doe&lt;/nachname&gt;<br />
&lt;belegungen&gt;<br />
&lt;belegung lehrveranstaltung="_12" note="1.0"/&gt;<br />
&lt;/belegungen&gt;<br />
&lt;/student&gt;<br />
&lt;/studenten&gt;<br />
&lt;lehrveranstaltungen&gt;<br />
&lt;lehrveranstaltung id="_12" belegungspunkte="6"&gt;<br />
&lt;titel&gt;Datenorientiertes XML&lt;/titel&gt;<br />
&lt;dozent&gt;Martin von Löwis&lt;/dozent&gt;<br />
&lt;beschreibung&gt;<br />
Das ist eine ganz nützliche Veranstaltung.<br />
&lt;/beschreibung&gt;<br />
&lt;/lehrveranstaltung&gt;<br />
&lt;/lehrveranstaltungen&gt;<br />
&lt;/studentenverwaltung&gt;<br />
</code></p>
<p>This whole document with two students will be read into 3 classes:</p>
<ul>
<li>Student,</li>
<li>Belegung and</li>
<li>Lehrveranstaltung</li>
</ul>
<p>These classes are quite small compared to Java classes:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Student<span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span> geburtstag
      <span style="color: #003399;">String</span> matrikelnummer
      <span style="color: #003399;">String</span> vorname
      <span style="color: #003399;">String</span> nachname
      def belegungen <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
      <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #0000ff;">&quot;Student: $vorname $nachname nMartrikelnummer: $matrikelnummer) nBelegungen: $belegungen&quot;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Belegung <span style="color: #009900;">&#123;</span>
	  Lehrveranstaltung lehrveranstaltung
	  <span style="color: #003399;">String</span> note
&nbsp;
	  <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		  <span style="color: #0000ff;">&quot;nLehrveranstaltung: $lehrveranstaltung  Note: $note&quot;</span>
&nbsp;
	  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Lehrveranstaltung <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span>  id
      <span style="color: #000066; font-weight: bold;">int</span>     belegungspunkte
      <span style="color: #003399;">String</span>  titel
      <span style="color: #003399;">String</span>  dozent
      <span style="color: #003399;">String</span>  beschreibung
&nbsp;
      <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	  <span style="color: #0000ff;">&quot;$titel ($dozent)&quot;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now the XML processing starts. At first we have to initialize the Namespace and load the XML file into a variable.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create namespace</span>
def ns <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> groovy.<span style="color: #006633;">xml</span>.<span style="color: #006633;">Namespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.hpi.uni-potsdam.de/studentenverwaltung&quot;</span>, <span style="color: #0000ff;">'ns'</span><span style="color: #009900;">&#41;</span>
def root <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'studies.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>After parsing the xml document it&#8217;s easy to iterate over the nodes with Groovy. A syntax like</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">root<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">studenten</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">student</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">each</span> <span style="color: #009900;">&#123;</span> xmlstudent <span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>reads all <strong>Stutent</strong> nodes within the <strong>Studenten</strong> node. The called each method iterates over the result list and provide the <em>xmlstudent</em> variable to access the current student within the following code block. With the help of this code block it is easy to read the student vales into a new object.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// read core data</span>
	student <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Student<span style="color: #009900;">&#40;</span>
		vorname<span style="color: #339933;">:</span>             xmlstudent<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">vorname</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
		nachname<span style="color: #339933;">:</span>           xmlstudent<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">nachname</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
		matrikelnummer<span style="color: #339933;">:</span>  xmlstudent.<span style="color: #0000ff;">'@matrikelnummer'</span>,
		geburtstag<span style="color: #339933;">:</span>          xmlstudent.<span style="color: #0000ff;">'@geburtstag'</span>
	<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The constructor behavior is special compared to Java. Groovy allows to initialize class members via the constructor. Additionally the instruction xmlstudent.&#8217;@matrikelnummer&#8217; reads the xml attribute <strong>matrikelnummer</strong> from the xml <strong>student</strong> node.</p>
<p>The second task is to read in all lecture assignments for the student. This task is more complex because the lectures are stored within it&#8217;s own node. For that reason we have to iterate over all assignments stored within the <strong>belegung</strong> nodes and use the assigned lecture link to find the right lecture.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// read belegungen</span>
xmlstudent<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">belegungen</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">belegung</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">each</span> <span style="color: #009900;">&#123;</span> belegung <span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To include the content of the lecture node we have to search for this lecture node now. The required lecture link is taken from the <strong>lehrveranstaltung</strong> attribute of the current assignment. This lecture lookup is written like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// read lecture</span>
xmlLehr <span style="color: #339933;">=</span> root<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">lehrveranstaltungen</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">lehrveranstaltung</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">find</span><span style="color: #009900;">&#123;</span>
	it.<span style="color: #0000ff;">'@id'</span> <span style="color: #339933;">==</span> belegung.<span style="color: #0000ff;">'@lehrveranstaltung'</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>With this lecture node it is possible to fill the student class with the student&#8217;s assignments.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// read assignment</span>
student.<span style="color: #006633;">belegungen</span> <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">new</span> Belegung<span style="color: #009900;">&#40;</span>
	note <span style="color: #339933;">:</span> belegung.<span style="color: #0000ff;">'@note'</span>,
	lehrveranstaltung <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">new</span> Lehrveranstaltung<span style="color: #009900;">&#40;</span>
		id					<span style="color: #339933;">:</span>xmlLehr.<span style="color: #0000ff;">'@id'</span>,
		belegungspunkte		<span style="color: #339933;">:</span>xmlLehr.<span style="color: #0000ff;">'@belegungspunkte'</span>,
		titel				<span style="color: #339933;">:</span>xmlLehr<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">titel</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
		dozent				<span style="color: #339933;">:</span>xmlLehr<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">dozent</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
		beschreibung		<span style="color: #339933;">:</span>xmlLehr<span style="color: #009900;">&#91;</span>ns.<span style="color: #006633;">beschreibung</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>These few lines of code are enough to read a namespace xml into Groovy. The iteration over all nodes and the simple attributes access is useful to keep the amount of code low. If you are interested to try out the whole example you are welcome to download the <a href="http://www.acidum.de/wp-content/uploads/2008/03/groovyxml.zip">attached zip file </a> that contains the Eclipse project with all required files.</p>
<p>Download:<br />
<a href="http://www.acidum.de/wp-content/uploads/2008/03/groovyxml.zip"><br />
<img src="http://www.acidum.de/wp-content/themes/acidum/images/www.gif" alt="Download" /><br />
Code Sample<br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/04/01/how-to-use-groovy-xml-parser-with-namespaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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