<?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>lstierneyltd &#187; jaxb</title>
	<atom:link href="http://lstierneyltd.com/blog/tag/jaxb/feed/" rel="self" type="application/rss+xml" />
	<link>http://lstierneyltd.com/blog</link>
	<description>Yet another development blog</description>
	<lastBuildDate>Wed, 13 Jul 2011 12:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Java to XML, XML to Java (Marshalling and Unmarshalling)</title>
		<link>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 12:55:13 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=273</guid>
		<description><![CDATA[Introduction JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial. Example Java to XML XML to Java product.java Note: the @XmlRootElement is vital here! product.xml]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial.</p>
<h3>Example</h3>
<h4>Java to XML</h4>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.math.BigDecimal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class JavaToXML {
	public static void main(String[] args) throws Exception {
		JAXBContext context = JAXBContext.newInstance(Product.class);

		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		Product object = new Product();
		object.setCode(&quot;WI1&quot;);
		object.setName(&quot;Widget Number One&quot;);
		object.setPrice(BigDecimal.valueOf(300.00));

		m.marshal(object, System.out);
	}
}
</pre>
<h4>XML to Java</h4>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class XMLToJava {

	public static void main(String[] args) {
		try {
			JAXBContext jc = JAXBContext.newInstance(Product.class);
			Unmarshaller u = jc.createUnmarshaller();

			File f = new File(&quot;product.xml&quot;);
			Product product = (Product) u.unmarshal(f);

			System.out.println(product.getCode());
			System.out.println(product.getName());
			System.out.println(product.getPrice());
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}
</pre>
<h4>product.java</h4>
<p>Note: the @XmlRootElement is vital here!</p>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Product {
	private String code;
	private String name;
	private BigDecimal price;

	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
}
</pre>
<h4>product.xml</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;product&gt;
    &lt;code&gt;WI1&lt;/code&gt;
    &lt;name&gt;Widget Number One&lt;/name&gt;
    &lt;price&gt;300.0&lt;/price&gt;
&lt;/product&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JAXB @XmlAnyElement example</title>
		<link>http://lstierneyltd.com/blog/development/tips/jaxb-xmlanyelement-example/</link>
		<comments>http://lstierneyltd.com/blog/development/tips/jaxb-xmlanyelement-example/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 15:06:52 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[quick tips]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=134</guid>
		<description><![CDATA[The problem I was recently working on a JAXB centric app which was required to capture &#8220;arbitary&#8221; xml. e.g. How could I ever capture the contents of if they could be &#8220;anything&#8221;? The solution What&#8217;s going on? The @XmlAnyElement annotation instructs JAXB to hoover up any elements which aren&#8217;t already annotated/associated with a field and [...]]]></description>
			<content:encoded><![CDATA[<h4>The problem</h4>
<p>I was recently working on a JAXB centric app which was required to capture &#8220;arbitary&#8221; xml. e.g.</p>
<pre class="brush: plain; title: ; notranslate">
&lt;a&gt;
   &lt;b&gt;&lt;/b&gt;
   &lt;c&gt;&lt;/c&gt;
   &lt;d&gt;
       &lt;!-- &quot;Unknown&quot; XML here --&gt;
       &lt;maybeE&gt;&lt;/maybeE&gt;
       &lt;maybeF&gt;&lt;/maybeF&gt;
       &lt;!-- etc etc --&gt;
   &lt;d/&gt;
&lt;/a&gt;
</pre>
<p>How could I ever capture the contents of <d> if they could be &#8220;anything&#8221;?<br />
<code><span id="more-134"></span></code></p>
<h4>The solution</h4>
<pre class="brush: plain; title: ; notranslate">
@XmlRootElement(name=&quot;a&quot;)
public class A { 

   @XmlElement
   private String b; 

   @XmlElement
   private String c; 

   @XmlAnyElement
   private List&lt;Element&gt; content;
}
</pre>
<h4>What&#8217;s going on?</h4>
<p>The <strong>@XmlAnyElement </strong> annotation instructs JAXB to hoover up any elements which aren&#8217;t already annotated/associated with a field and store their DOM representation in:</p>
<pre>
private List<Element> content;
</pre>
<p>You could then do something like:</p>
<pre class="brush: plain; title: ; notranslate">
public String getContentAsString() throws Exception{
    StringBuilder builder = new StringBuilder();
    for (Node node: operations) {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, &quot;yes&quot;);
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        builder.append(writer.toString());
    }
    return builder.toString();
}
</pre>
<p>To get the nested XML as String.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/tips/jaxb-xmlanyelement-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

