Related MOXy Functionality
MOXy currently supports writing the mapped properties of a child object directly into the parent objects element using @XmlPath(".")
(see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html).
What's Interesting About Your Use Case
What makes it tricky with your use case is that you have an added level of nesting in your example. We don't support this directly today, but I have added an enhancement request (see: http://bugs.eclipse.org/432029). When this is implemented you will be able to do something like:
@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class wrapper {
@XmlPath("body")
private Object1 one;
@XmlPath("body")
private Object2 two;
@XmlPath("body")
private Object3 thr;
}
Work Around
With current functionality you could do the following:
Java Model
@XmlRootElement(name="body")
@XmlAccessorType(XmlAccessType.FIELD)
public class wrapper {
@XmlPath(".")
private Object1 one;
@XmlPath(".")
private Object2 two;
@XmlPath(".")
private Object3 thr;
}
Demo
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource source = new StreamSource("input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(source);
while(!(xsr.isStartElement() && "body".equals(xsr.getLocalName()))) {
xsr.next();
}
JAXBContext jc = JAXBContext.newInstance(wrapper.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
wrapper result = (wrapper) unmarshaller.unmarshal(xsr);
}
}
For More Information