gudii9
asked on
custom wrapper object jaxb
what is custom wrapper object? which scenario we have to use? is it is similar to XSD?
https://jaxb.java.net/tutorial/section_6_2_7_7-Wrapping-Repeated-Elements-XmlElementWrapper.html
Please advise
https://jaxb.java.net/tutorial/section_6_2_7_7-Wrapping-Repeated-Elements-XmlElementWrapper.html
Please advise
ASKER
are they used for unmarshalling also?
please advise.
where xjc coming into picture in this?
please advise.
where xjc coming into picture in this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Xjc is simply the binding compiler. Which can compile an xsd into java classes with JAXB.
is it alternate way of doing unmarshalling directly without writing the code like below
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import blog.thoughts.on.java.Item;
public class UnmarshallingDemo {
public static void main (String [] args) {
try {
JAXBContext jc = JAXBContext.newInstance ("blog.thoughts.on.java");
Unmarshaller u = jc.createUnmarshaller ();
File f = new File ("item.xml");
JAXBElement element = (JAXBElement) u.unmarshal (f);
Item item = (Item) element.getValue ();
System.out.println (item.getCode ());
System.out.println (item.getName ());
System.out.println (item.getPrice ());
} catch (JAXBException e) {
e.printStackTrace ();
}
}
They're simply used for collections when using jaxb to marshall. Since collection classes are not known to the jaxb context, you create a wrapper object that is annotated with jaxb and let it wrap the underlying collection.