<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: A few Java snippets I keep reinventing	</title>
	<atom:link href="http://matteo.vaccari.name/blog/archives/124/feed" rel="self" type="application/rss+xml" />
	<link>http://matteo.vaccari.name/blog/archives/124</link>
	<description>Extreme enthusiasm</description>
	<lastBuildDate>
	Mon, 25 Feb 2019 15:18:16 +0000	</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.1.1</generator>
			<item>
				<title>
				By: matteo				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-96311</link>
		<dc:creator><![CDATA[matteo]]></dc:creator>
		<pubDate>Tue, 03 Sep 2013 15:05:01 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-96311</guid>
					<description><![CDATA[How to transform a SOAP response from XML to Java object:

&lt;pre&gt;
protected &#060;T&#062; T unmarshal(Class&#060;T&#062; klass, String fileName) throws JAXBException
{
    JAXBContext context = JAXBContext.newInstance(klass.getPackage().getName());
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream stream = this.getClass().getResourceAsStream(fileName);
    JAXBElement jaxbElement = (JAXBElement) unmarshaller.unmarshal(stream);
    return (T) jaxbElement.getValue();
}
&lt;/pre&gt;

How to do the reverse:

&lt;pre&gt;
// See http://stackoverflow.com/questions/2472155/i-want-to-convert-an-output-stream-into-string-object
public &#060;T&#062; String marshal(Class &#060;T&#062; klass, T object) throws JAXBException
{
  StringWriter writer = new StringWriter();
  JAXBContext context = JAXBContext.newInstance(klass);
  Marshaller marshaller = context.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_ENCODING, &#034;UTF-8&#034;);
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  JAXBElement jaxbElement = new JAXBElement&#060;T&#062;(new QName(&#034;&#034;,&#034;rootTag&#034;), klass, object);
  marshaller.marshal(jaxbElement, writer);
  return writer.toString();
}
&lt;/pre&gt;]]></description>
		<content:encoded><![CDATA[<p>How to transform a SOAP response from XML to Java object:</p>
<pre>
protected &lt;T&gt; T unmarshal(Class&lt;T&gt; klass, String fileName) throws JAXBException
{
    JAXBContext context = JAXBContext.newInstance(klass.getPackage().getName());
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream stream = this.getClass().getResourceAsStream(fileName);
    JAXBElement jaxbElement = (JAXBElement) unmarshaller.unmarshal(stream);
    return (T) jaxbElement.getValue();
}
</pre>
<p>How to do the reverse:</p>
<pre>
// See http://stackoverflow.com/questions/2472155/i-want-to-convert-an-output-stream-into-string-object
public &lt;T&gt; String marshal(Class &lt;T&gt; klass, T object) throws JAXBException
{
  StringWriter writer = new StringWriter();
  JAXBContext context = JAXBContext.newInstance(klass);
  Marshaller marshaller = context.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_ENCODING, &quot;UTF-8&quot;);
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  JAXBElement jaxbElement = new JAXBElement&lt;T&gt;(new QName(&quot;&quot;,&quot;rootTag&quot;), klass, object);
  marshaller.marshal(jaxbElement, writer);
  return writer.toString();
}
</pre>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: matteo				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-70580</link>
		<dc:creator><![CDATA[matteo]]></dc:creator>
		<pubDate>Sun, 05 Oct 2008 19:54:07 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-70580</guid>
					<description><![CDATA[Mirko: you&#039;re right.  I corrected the error.  Although a better fix would be to use Joda Time (http://joda-time.sourceforge.net/)!]]></description>
		<content:encoded><![CDATA[<p>Mirko: you&#8217;re right.  I corrected the error.  Although a better fix would be to use Joda Time (<a href="http://joda-time.sourceforge.net/" rel="nofollow">http://joda-time.sourceforge.net/</a>)!</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: Mirko				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-67646</link>
		<dc:creator><![CDATA[Mirko]]></dc:creator>
		<pubDate>Sat, 26 Jul 2008 12:01:01 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-67646</guid>
					<description><![CDATA[Only a little thing: 
SimpleDateFormat is not thread-safe, so need to be instantiated every time.]]></description>
		<content:encoded><![CDATA[<p>Only a little thing:<br />
SimpleDateFormat is not thread-safe, so need to be instantiated every time.</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: riffraff				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-67640</link>
		<dc:creator><![CDATA[riffraff]]></dc:creator>
		<pubDate>Sat, 26 Jul 2008 09:59:56 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-67640</guid>
					<description><![CDATA[interesting, I used items #1 and #2 yesterday and #5 is already in the codebase, albeit I prefer to transform the NodeList into a List :)]]></description>
		<content:encoded><![CDATA[<p>interesting, I used items #1 and #2 yesterday and #5 is already in the codebase, albeit I prefer to transform the NodeList into a List :)</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: matteo				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-67572</link>
		<dc:creator><![CDATA[matteo]]></dc:creator>
		<pubDate>Fri, 25 Jul 2008 14:47:25 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-67572</guid>
					<description><![CDATA[You&#039;re welcome!  Some of this stuff is from the old Milano-XPUG projects...]]></description>
		<content:encoded><![CDATA[<p>You&#8217;re welcome!  Some of this stuff is from the old Milano-XPUG projects&#8230;</p>
]]></content:encoded>
						</item>
						<item>
				<title>
				By: fabiobeta				</title>
				<link>http://matteo.vaccari.name/blog/archives/124/comment-page-1#comment-67571</link>
		<dc:creator><![CDATA[fabiobeta]]></dc:creator>
		<pubDate>Fri, 25 Jul 2008 14:18:30 +0000</pubDate>
		<guid isPermaLink="false">http://matteo.vaccari.name/blog/?p=124#comment-67571</guid>
					<description><![CDATA[Matteo...grazie!!!]]></description>
		<content:encoded><![CDATA[<p>Matteo&#8230;grazie!!!</p>
]]></content:encoded>
						</item>
			</channel>
</rss>
