<?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>off-topic &#187; XStream</title>
	<atom:link href="http://off-topic.biz/en/tag/xstream/feed/" rel="self" type="application/rss+xml" />
	<link>http://off-topic.biz/en</link>
	<description>A collection of off-topic posts</description>
	<lastBuildDate>Thu, 07 Apr 2011 09:16:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>ConversionException when parsing XML with XStream: Element is not defined</title>
		<link>http://off-topic.biz/en/conversionexception-when-parsing-xml-with-xstream-element-is-not-defined/</link>
		<comments>http://off-topic.biz/en/conversionexception-when-parsing-xml-with-xstream-element-is-not-defined/#comments</comments>
		<pubDate>Wed, 27 May 2009 12:04:00 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XML Parser]]></category>
		<category><![CDATA[XStream]]></category>

		<guid isPermaLink="false">http://off-topic.biz/en/?p=39</guid>
		<description><![CDATA[XStream is an easy to use Java library to serialize objects to XML and back again. Yesterday I wanted to parse a XML document generated by a third party software with the following structure: &#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62; &#60;AuditTrail&#62; &#60;Entry type=&#34;ExecutionReport&#34; &#8230; <a href="http://off-topic.biz/en/conversionexception-when-parsing-xml-with-xstream-element-is-not-defined/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://xstream.codehaus.org/">XStream</a> is an easy to use Java library to serialize objects to XML and back again. Yesterday I wanted to parse a XML document generated by a third party software with the following structure:</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;AuditTrail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Entry</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ExecutionReport&quot;</span> <span style="color: #000066;">msgId</span>=<span style="color: #ff0000;">&quot;2544804979&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;35&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;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;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;34&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;000023&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;43&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;N&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;52&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;20090526-20:08:31&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;6556&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;today12&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;17&quot;</span> <span style="color: #000066;">val</span>=<span style="color: #ff0000;">&quot;73740.1243368511.0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">tag</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">val</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;/Entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Entry</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Acknowledged&quot;</span> <span style="color: #000066;">msgId</span>=<span style="color: #ff0000;">&quot;1540251818&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/AuditTrail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I created three classes <code>AuditTrail</code>, <code>AuditTrailEntry</code> and <code>AuditTrailEntryField</code> for the elements <code>AuditTrail</code>, <code>Entry</code>, and <code>field</code>, respectively. In addition, I set the aliases accordingly:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">XStream xStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XStream <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xStream.<span style="color: #006633;">alias</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AuditTrail&quot;</span>, AuditTrail.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xStream.<span style="color: #006633;">alias</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Entry&quot;</span>, AuditTrailEntry.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xStream.<span style="color: #006633;">alias</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;field&quot;</span>, AuditTrailEntryField.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A call to the method <code>fromXml()</code> of the <code>XStream</code> object resulted in the following exception:</p>
<pre>
ConversionException: Element [NAME] of type [TYPE]
                     is not defined as [NAME] in type [TYPE]
</pre>
<p>Because I did not find a quick solution via a Google search I was forced to read the XStream documentation. ;-) There I learned something about <a href="http://xstream.codehaus.org/annotations-tutorial.html#ImplicitCollections">Implicit Collections</a> in XStream. Whenever you have a collection which does not display it&#8217;s root tag, you can map it as an implicit collection:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">xStream.<span style="color: #006633;">addImplicitCollection</span> <span style="color: #009900;">&#40;</span>AuditTrailEntry.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;fields&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xStream.<span style="color: #006633;">addImplicitCollection</span> <span style="color: #009900;">&#40;</span>AuditTrail.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;entries&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>with</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> <span style="color: #339933;">&lt;</span>AuditTrailEntryField<span style="color: #339933;">&gt;</span> fields<span style="color: #339933;">;</span></pre></div></div>

<p>in class <code>AuditTrailEntry</code> and</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> <span style="color: #339933;">&lt;</span>AuditTrailEntry<span style="color: #339933;">&gt;</span> entries<span style="color: #339933;">;</span></pre></div></div>

<p>in class <code>AuditTrail</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://off-topic.biz/en/conversionexception-when-parsing-xml-with-xstream-element-is-not-defined/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

