1
votes

I hava the following structure:

<data name="piece-shipment" error-status="0"
        product-name="test" name="Hook" street="Freeway 21" city="Bumphill"/>

and the following jaxb annotated model..

@XmlRootElement(name = "data")
@XmlAccessorType(XmlAccessType.FIELD)    
public class PieceShipment {

    /**
     * The field describes, of the parcel is returned to the origin sender
     */
    @XmlAttribute(name = "product-name")
    private String productName;

    /**
     * recipient information
     */
    @XmlJavaTypeAdapter(RecipientAdapter.class)
    private Recipient recipient;
   .....
}




 @XmlAccessorType(XmlAccessType.FIELD)
public class Recipient {

    private String name;
    private String street;
    private String city;
...
}

How can I map the relation of PieceShipment to Recipient? They are on the same element level.

I've tried to use Adapters, but the adapter doesn't get called.

1
This is something you can't do with JAXB. You don't have a single XML element that can be adapted to another type. The problem is with the original XML structure which isn't relational-modelled.laune
So there is no way to map the attributes relational?aphex
Look at it this way: having a recipient with (hopefully) name as the key: what would you do with differing address properties? And if it could be mapped, you'd end up with oodles of Recipient objects all over the place. - Best to keep it as it is, run some processing after unmarshalling everything to extract and check recipient data; handle duplicate Smiths in different cities or even same city, etc.laune

1 Answers

1
votes

Using the standard JAXB (JSR-222) mapping metadata you can't have the properties from Recipient mapped into the same parent element that PieceShipment is mapped to.

If you are using EclipseLink MOXy as your JAXB provider then you can use our @XmlPath extension to accomplish this:

@XmlPath(".")
private Recipient recipient;

For More Information

I have written more about MOXy's XPath mapping extension on my blog: