<?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>teamzone.ch &#187; Maven</title>
	<atom:link href="http://teamzone.ch/category/maven/feed/" rel="self" type="application/rss+xml" />
	<link>http://teamzone.ch</link>
	<description>Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.</description>
	<lastBuildDate>Sat, 05 Jun 2010 13:23:27 +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>Getting in touch with JiBX and Maven</title>
		<link>http://teamzone.ch/2008/06/08/getting-in-touch-with-jibx-and-maven/</link>
		<comments>http://teamzone.ch/2008/06/08/getting-in-touch-with-jibx-and-maven/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 19:59:25 +0000</pubDate>
		<dc:creator>Stefan Jäger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://blog.stefanjaeger.ch/2008/06/08/getting-in-touch-with-jibx-and-maven/</guid>
		<description><![CDATA[
JiBX is one of the fastest XML object mapping frameworks out there (check out this performance test). Furthermore, it is very flexible in binding XML structures to objects. In this blog post I will describe, how a basic JiBX project can be set up with Maven2. The focus of this entry is on getting in [...]]]></description>
			<content:encoded><![CDATA[</p>
<p><a href="http://jibx.sourceforge.net/">JiBX</a> is one of the fastest XML object mapping frameworks out there (check out <a href="http://www.ibm.com/developerworks/library/x-databdopt2/">this performance test</a>). Furthermore, it is very flexible in binding XML structures to objects. In this blog post I will describe, how a basic JiBX project can be set up with Maven2. The focus of this entry is on getting in touch with JiBX (and a little bit with Maven).<br />To use JiBX in our project, we just have to add the dependency to our pom.xml. JiBX is fast, but to get it&#8217;s high performance, it uses byte code enhancement. For this reason, the byte code need&#8217;s to be edited by JiBX after the compilation phase. This is done with a Maven JiBX plugin, which is executed in the build phase &#8220;process-classes&#8221;.
<p>Besides, I also wanted a JAR file, which contains all dependencies and has a defined main class (to which I will come to later). The creation of the JAR file is done with the Maven Assembly.
<p>Check out the <a href="http://teamzone.ch/wp-content/jibxtestrun_pom.xml">pom.xml</a> for this test run.
<p>After setting up the <a href="http://teamzone.ch/wp-content/jibxtestrun_pom.xml">pom.xml</a>, we can begin to develop a mapping example with Eclipse. JiBX has two main parts on which we have to focus, binding and the runtime part. <br />In the binding part, JiBX enhances the byte code (based on a binding definition) to enable the mapping between Java objects and a XML files. In the runtim part, the JiBX runtime components are used to perform the marshal (objects to XML) or unmarshal (XML to objects) tasks.
<p>As you have seen in the <a href="http://teamzone.ch/wp-content/jibxtestrun_pom.xml">pom.xml</a>, the JiBX binding files should be located in src/main/jibx. The names of the JiBX files should end with -binding.xml.
<p>Our goal is to map an XML file, based on following schema CustomerSchema.xsd:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;schema</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;http://stefanjaeger.ch/CustomerSchema&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:sja</span>=<span style="color: #ff0000;">&quot;http://stefanjaeger.ch/CustomerSchema&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">elementFormDefault</span>=<span style="color: #ff0000;">&quot;qualified&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;customer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sja:person&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sja:city&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;person&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sja:first-name&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sja:last-name&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;first-name&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;last-name&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;city&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/schema<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Our XML Customer1.xml file looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customer</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://stefanjaeger.ch/CustomerSchema&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://stefanjaeger.ch/CustomerSchema CustomerSchema.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;person<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;first-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Stefan<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/first-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;last-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jaeger<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/last-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/person<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Davos Dorf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/city<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We want to map the XML in the two objects Customer and Person (to simplify these lines of code, the getter, setter and toString methods are not described).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Person person<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> city<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To map the XML file to these classes, we create following binding definition. Because we use a XML schema, we have to define the namespace. Also the fully qualified name of the mapped class Customer need&#8217;s to be used. Because the class Person is in relation with the Customer class, JiBX recognizes automatically the fully qualified name of Person.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;customer&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.stefanjaeger.jibx.Customer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespace</span> <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://stefanjaeger.ch/CustomerSchema&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;elements&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;structure</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;person&quot;</span> <span style="color: #000066;">field</span>=<span style="color: #ff0000;">&quot;person&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;first-name&quot;</span> <span style="color: #000066;">field</span>=<span style="color: #ff0000;">&quot;firstName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;last-name&quot;</span> <span style="color: #000066;">field</span>=<span style="color: #ff0000;">&quot;lastName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/structure<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;city&quot;</span> <span style="color: #000066;">field</span>=<span style="color: #ff0000;">&quot;city&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now, we have a XML file, some Java classes and a binding between them. What we now need is an application, which does the unmarshalling of the XML file.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JiBXDemoApplication <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    IBindingFactory bfact <span style="color: #339933;">=</span> BindingDirectory.<span style="color: #006633;">getFactory</span><span style="color: #009900;">&#40;</span>Customer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    IUnmarshallingContext uctx <span style="color: #339933;">=</span> bfact.<span style="color: #006633;">createUnmarshallingContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Customer customer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Customer<span style="color: #009900;">&#41;</span> uctx.<span style="color: #006633;">unmarshalDocument</span><span style="color: #009900;">&#40;</span>
      <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Customer1.xml&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</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>customer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>We are finished now. Just build the project with the command <strong>mvn package</strong> and let&#8217;s see, how the test run is working. Remember to place the XML schema and the XML file to the same location as the JAR file (after mvn package, it should be located in /target/). </p>
<p>The command </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java <span style="color: #660033;">-jar</span> jibx_playground-<span style="color: #000000;">1.0</span>-SNAPSHOT-jar-with-dependencies.jar</pre></div></div>

</p>
<p>results in </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ch.stefanjaeger.jibx.Customer<span style="color: #000000; font-weight: bold;">@</span>12d03f9
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">person</span>=ch.stefanjaeger.jibx.Person<span style="color: #000000; font-weight: bold;">@</span>15dfd77
 <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">firstName</span>=Stefan,<span style="color: #007800;">lastName</span>=Jaeger<span style="color: #7a0874; font-weight: bold;">&#93;</span>
,<span style="color: #007800;">city</span>=Davos Dorf<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

</p>
<p>I have to admit, that I did the same example with ANT. It tooks me a little bit longer… From today on, I’m a Maven fan <img src='http://teamzone.ch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><a href="http://teamzone.ch/wp-content/jibxtestrun.zip">Download here the source code of this example</a>. You only have to run mvn package to build the project. </p>
]]></content:encoded>
			<wfw:commentRss>http://teamzone.ch/2008/06/08/getting-in-touch-with-jibx-and-maven/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
