Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

XSD and wrappers are not related.

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.
Avatar of gudii9

ASKER

are they used for unmarshalling also?

please advise.

where xjc coming into picture in this?
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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 ();
       }
   }

Open in new window