<?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>Adams Bros Blog &#187; XML</title>
	<atom:link href="http://blog.adamsbros.org/category/programming/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adamsbros.org</link>
	<description></description>
	<lastBuildDate>Sat, 14 Aug 2010 07:30:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What is JAXB and JAXB Example Code</title>
		<link>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/</link>
		<comments>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:12:28 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=200</guid>
		<description><![CDATA[I thought it may be nice for those googlers that want to know about JAXB, to have a quick working example to use.  So, that is the purpose of this post.
First off, what is JAXB?  Well, simply put, JAXB is for converting Java objects to XML or from XML to Java objects.  With the advent [...]]]></description>
			<content:encoded><![CDATA[<p>I thought it may be nice for those <a title="Googlers" href="http://www.google.com">googlers</a> that want to know about JAXB, to have a quick working example to use.  So, that is the purpose of this post.</p>
<p><span id="more-200"></span>First off, what is JAXB?  Well, simply put, JAXB is for converting Java objects to XML or from XML to Java objects.  With the advent of Java 1.5, and annotations, this becomes extremely simple.</p>
<p>Download <a title="JAXB Download" href="http://www.google.com/search?q=jaxb+download">JAXB</a>, dependencies to your local computer, and put them in a folder called "libs". The dependencies JARs I have are...</p>
<ul>
<li>activation-1.1.jar</li>
<li>jaxb-api-2.0.jar</li>
<li>jaxb-impl-2.0.5.jar</li>
<li>jsr173_api-1.0.jar</li>
</ul>
<p>Copy the following Java source code to Employee.java, and proceed with the instructions below</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.JAXBContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.JAXBException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
&nbsp;
@XmlRootElement<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;employee&quot;</span><span style="color: #009900;">&#41;</span>
@XmlAccessorType<span style="color: #009900;">&#40;</span>XmlAccessType.<span style="color: #006633;">FIELD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Employee
<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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JAXBException
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Employee john <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setMiddleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Robert&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        john.<span style="color: #006633;">setLastName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Doe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// write it out as XML</span>
        <span style="color: #000000; font-weight: bold;">final</span> JAXBContext jaxbContext <span style="color: #339933;">=</span> JAXBContext.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>Employee.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">StringWriter</span> writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        jaxbContext.<span style="color: #006633;">createMarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">marshal</span><span style="color: #009900;">&#40;</span>john, writer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// read it from XML</span>
        <span style="color: #000000; font-weight: bold;">final</span> Employee johnRead <span style="color: #339933;">=</span>
            <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> jaxbContext.<span style="color: #006633;">createUnmarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">unmarshal</span><span style="color: #009900;">&#40;</span>
                <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringReader</span><span style="color: #009900;">&#40;</span>writer.<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>john.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>johnRead<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// write the new object out as XML again.</span>
            writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            jaxbContext.<span style="color: #006633;">createMarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">marshal</span><span style="color: #009900;">&#40;</span>johnRead, writer<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;johnRead was identical to john: <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span> writer.<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: #000000; font-weight: bold;">else</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;john and johnRead are not equal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @XmlAttribute
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's first name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's middle name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> middleName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Employee's last name
     */</span>
    @XmlElement
    <span style="color: #000000; font-weight: bold;">private</span>
    <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> lastName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMiddleName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> middleName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMiddleName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> middleName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">middleName</span> <span style="color: #339933;">=</span> middleName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> firstName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFirstName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> o<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> o.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        Employee employee <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>id <span style="color: #339933;">!=</span> employee.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>firstName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>firstName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">firstName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">firstName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lastName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>lastName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">lastName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">lastName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>middleName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>middleName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>employee.<span style="color: #006633;">middleName</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            employee.<span style="color: #006633;">middleName</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>firstName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> firstName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>middleName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> middleName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>lastName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> lastName.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <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: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Employee{&quot;</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;id=&quot;</span> <span style="color: #339933;">+</span> id <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, firstName='&quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, middleName='&quot;</span> <span style="color: #339933;">+</span> middleName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">&quot;, lastName='&quot;</span> <span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span>
            <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The following commands will compile and run the source, if under a unix OS.  If in windows, you'll need to form your own classpath.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac <span style="color: #660033;">-classpath</span> .:$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> jar <span style="color: #000000; font-weight: bold;">in</span> libs<span style="color: #000000; font-weight: bold;">/*</span>.jar; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$jar</span>:&quot;</span>; <span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> Employee.java
java <span style="color: #660033;">-classpath</span> .:$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> jar <span style="color: #000000; font-weight: bold;">in</span> libs<span style="color: #000000; font-weight: bold;">/*</span>.jar; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$jar</span>:&quot;</span>; <span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> Employee</pre></div></div>

<p>The output should be as follows, but the XML will be on a single line...</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">johnRead was identical to john<span style="color: #339933;">:</span>
<span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span> standalone<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>employee id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>firstName<span style="color: #339933;">&gt;</span>John<span style="color: #339933;">&lt;/</span>firstName<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>middleName<span style="color: #339933;">&gt;</span>Robert<span style="color: #339933;">&lt;/</span>middleName<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>lastName<span style="color: #339933;">&gt;</span>Doe<span style="color: #339933;">&lt;/</span>lastName<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>employee<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2010/02/07/jaxb-example-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>XSL Break or Wrap String on Word Boundary</title>
		<link>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/</link>
		<comments>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 23:00:16 +0000</pubDate>
		<dc:creator>Trenton</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://blog.adamsbros.org/?p=93</guid>
		<description><![CDATA[I've searched all over the Internet for this, and was unable to find anything reasonable.  I found an example somewhere, of how to break a string at a specific location, but it breaks whether there is a word there or not.  So, either you have to re-compose the XML elements without a space, and hope [...]]]></description>
			<content:encoded><![CDATA[<p>I've searched all over the Internet for this, and was unable to find anything reasonable.  I found an example somewhere, of how to break a string at a specific location, but it breaks whether there is a word there or not.  So, either you have to re-compose the XML elements without a space, and hope every system you interact with does the same thing as you,  or re-compose them with a space, and a word may then be broken up in the end result.</p>
<p>In my example below, I break a string on a word boundary, outputting to an XML element called "NoteMessage" from the PESC standard.  This is dependant on Java, but you could use any language that has a useful lastIndexOf function.  In the case of Java, with zero based string index, we need to compensate for the one based string index that XSL has.  So, we add one to the result of the lastIndexOf() call.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">Example...</span>
<span style="color: #808080; font-style: italic;">java -cp target/dependency/xml-util-0.1.3-SNAPSHOT.jar:/usr/share/xalan/lib/xalan.jar \</span>
<span style="color: #808080; font-style: italic;">org.apache.xalan.xslt.Process -XSL target/classes/xsl/notemessage.xsl \</span>
<span style="color: #808080; font-style: italic;"> -PARAM testString &quot;This is a test to break a string into multiple note messages, \</span>
<span style="color: #808080; font-style: italic;">automatically, without programming.&quot;</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">xmlns:String</span>=<span style="color: #ff0000;">&quot;http://xml.apache.org/xalan/java/java.lang.String&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">indent</span>=<span style="color: #ff0000;">&quot;${xml.indent}&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">omit-xml-declaration</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testString&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;break-at&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;'76'&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$testString&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</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;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;string-length($string) &lt;= $break-at&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NoteMessage&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</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;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- call method to find word boundary index --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;truncString&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;String:new(substring($string, 1, $break-at))&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;lastSpaceIndex&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;String:lastIndexOf($truncString, ' ') + 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;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NoteMessage&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring(string($truncString), 1, $lastSpaceIndex)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;note-message&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;string&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring($string, $lastSpaceIndex + 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;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.adamsbros.org/2009/11/22/xsl-break-string-on-word-boundary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
