users@jaxb.java.net

Re: Marshalling and unmarshalling to several root elements

From: Han Ming Ong <hanming_at_mac.com>
Date: Wed, 07 May 2003 10:34:17 -0700

Besides the partial-unmarshalling approach that Kohsuke recommends,
another approach that I often take is to use XSLT to transfer your
example 2 into example 1, before feeding the XML to JAXB. In this case,
your JAXB classes and schema is cleaner and simpler.

here's a XSLT fragment (without testing but probably pretty close)

<xsl:template match="/car">
<cars>
   <xsl:copy-of select="."/>
<cars>
</xsl:template>

Look at the javadoc for javax.xml.bind.util.JAXBResult to see how they
interoperate. You may have to work around bugs (I know I have but I
don't have the bug numbers handy. Don't be frustrated :-)

Han Ming

On Wednesday, May 7, 2003, at 03:07 AM, Rózsás László wrote:

> Hi,
>  
> I have an XML schema with a root element called for example <cars>,
> that can contain several <car> tags, that in turn contain the
> description
> of a car. In some cases I only need  the description of one car, so I'd
> like to get an XML file that contains only one <car> tag as its root
> element, but in some other cases I need the complete list of cars with
> their descriptions, so I'd like to generate an XML file with a <cars>
> tag
> as root element. How can I do that in JAXB?
>  
> Example 1:
>  
> <cars>
>   <car>
>     <type></type>
>   </car>
>   <car>
>     <type></type>
>   </car>
>   <car>
>     <type></type>
>   </car>
> .....
> </cars>
>  
> Example 2:
>  
> <car>
>   <type></type>
> </car>
>  
> Thanks,
>  
> Laszlo Rozsas