Question

I am using MOXy and same domain object model to produce xml/json. Marshalling and unmarhsalling to produce json output works as expected, I could get java object with all values after unmarshalling but unmarshalling the xml doesnt gives a java object with all expected values instead gives null values. Though marshalling works fine. Below is the code

Domain Object model

@XmlType(propOrder = {"colour","model","transmission"})
public class BMW {

private String colour;
private String model;
private String transmission;

@XmlPath("Cars/BMW/colour/text()")
public void setColour(String colour){
    this.colour = colour;       
}
@XmlPath("Cars/BMW/model/text()")
public void setModel(String model){
    this.model = model;     
}
@XmlPath("Cars/BMW/transmission/text()")
public void setTransmission(String transmission){
    this.transmission = transmission;       
}

public String getColour(){
    return this.colour;
}
public String getModel(){
    return this.model;
}
public String getTransmission(){
    return this.transmission;
}

}

Test Method for marshalling and Unmarshalling

    BMW bmw = new BMW();
    bmw.setColour("white");
    bmw.setModel("X6");
    bmw.setTransmission("AUTO");
    File fileXML = new File("/..../bmw.xml");
    File fileJson = new File("/..../bmw.json");
    XMLInputFactory xif = XMLInputFactory.newInstance();
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(BMW.class);
        Marshaller m = jaxbContext.createMarshaller();
        Unmarshaller um = jaxbContext.createUnmarshaller();         
//=====         XML
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(bmw, fileXML);
        m.marshal(bmw, System.out); 
        StreamSource xml = new StreamSource(fileXML);
        XMLStreamReader xsr = xif.createXMLStreamReader(xml);
//          xsr.nextTag();
//          xsr.nextTag();
        BMW bmwXML = (BMW)um.unmarshal(xsr,BMW.class).getValue();

//====          JSON
        m.setProperty("eclipselink.json.include-root", false);
        m.setProperty("eclipselink.media-type", "application/json");         
        um.setProperty("eclipselink.media-type", "application/json");
        um.setProperty("eclipselink.json.include-root", false);  
        m.marshal(bmw, fileJson);
        m.marshal(bmw, System.out);
        StreamSource json = new StreamSource(fileJson)
        BMW bmwJson = um.unmarshal(json, BMW.class).getValue();         
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

As you can see from the code above I've tried using xsr.NextTag() but didn't help.

So the bmwXML have all null values while bmwJson works fine. Not sure what I am doing wrong.

Was it helpful?

Solution

You haven't supplied root element information for your BMW class when marshalling to XML. This means you need to do one of the following:

  1. Annotate BMW with @XmlRootElement

    @XmlRootElement
    @XmlType(propOrder = {"colour","model","transmission"})
    public class BMW {
    
  2. Wrap your instance of BMW in a JAXBElement before marshalling it. Note when you did the unmarshal the instance of BMW was wrapped in a JAXBElement, this is what you called getValue() on.

    JAXBElement<BMW> je = new JAXBElement(new QName("root-element-name"), BMW.class, bmw);
    m.marshal(je, fileXML);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top