Given the following XML
<mappings>
<map>
<source srcAttr="oof">foo</source>
<target trgAttr="rab">bar</target>
</map>
<map>
...
Is it possible with JAXB to unmarshal the <map>
elements into a single class Map
containing values and attributes of <source>
and <target>
?
@XmlRootElement
class Map {
@XmlElement
String source;
@???
String srcAttr;
@XmlElement
String target;
@???
String trgAttr;
}
I don't want to create extra classes for Source and target.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
You could use MOXy's @XmlPath
extension to handle this use case:
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Map {
String source;
@XmlPath("source/@srcAttr")
String srcAttr;
String target;
@XmlPath("target/@trgAttr")
String trgAttr;
}
For More Information
Yes! Just replace ???
with @XmlAttribute
annotaion.
Also this might be helpful jaxb example and this oracle examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With