1
votes

Is it possible to read the same element into two different Java properties using EclipseLink MOXy?

In the below example I'm mapping the <publication> document as a class, and the <date> element as Java class with an XmlAdapter that's a propert of my publication object. I'd also like to have the year element set as a separate property of the Publication class, as well as part of the compound Date class I've made. However, when I set this up in the XML bindings, year alone is always null, but the date gets populated properly.

<publication>
    <date datetype="OriginalPub">
        <year>2011</year>
        <month>Feb.</month>
        <day>0</day>
    </date>
</publication
1

1 Answers

2
votes

Currently EclipseLink JAXB (MOXy) does not support the XPath for mapping one classes properties to dip into the portion of an XML document that is mapped to another class. You could leverage an afterUnmarshal event to pull the year from the date object and set it on the publication object.

package forum13122968;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Publication {

    Date date;
    int year;

    private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
        year = date.getYear();
    }

}