users@jaxb.java.net

Re: getting POJO attributes as element content?

From: Levi Purvis <javanet_at_purvis.ws>
Date: Wed, 22 Nov 2006 14:44:59 -0500

Try annotating like so:

Garage {

   @XmlElement(name="Car")
   getCars()
}

Car {

   @XmlValue
   getName()
}

Since Car extends Vehicle, you'll need to put @XmlTransient on the
Vehicle class in order for the @XmlValue to work (you'll also need to
use JAXB 2.1 EA).


On 11/22/06, Jamie wrote:
> Hi -
>
> I'm in need of some guidance on how to solve a couple of problems I'm
> running into using JAXB to marshal our POJOs to meet an existing schema.
> I'll send out a separate email for the other issue, but one of the
> problems I'm running into is I have code like this:
>
> Class Garage {
> List<Car> getCars() {
> return carList;
> }
> }
>
> Class Car extends Vehicle {
> String getName() {
> return "Honda Accord";
> }
> int getNumberOfDoors() {
> ...
> }
> }
>
> I want the Xml snippet for this to marshal out to:
> <Garage>
> <Car>Honda Accord</Car>
> </Garage>
>
> I know that one way to do it is to annotate getCars as transient, write
> a new method getCarNames and annotate that to the Car element... But I'm
> looking for a hopefully more elegant solution simply involving
> annotating my existing class(es).
>
>
> Thanks!
> -Jamie