Search code examples
javajaxbjaxb2

XML to Object Unmarshalling using JAXB


I need to Unmarshal XML to Java Object, I have tried with below code. Its create an Object but set all value as a null. code for same:

 @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Dispatch implements java.io.Serializable {
    private Integer dispatchId;
    private Order order;

    /**
     * @return the dispatchId
     */
    public Integer getDispatchId() {
        return dispatchId;
    }

    /**
     * @param dispatchId
     *            the dispatchId to set
     */
    public void setDispatchId(Integer dispatchId) {
        this.dispatchId = dispatchId;
    }

    /**
     * @return the order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @param order
     *            the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return ""+this.dispatchId;
    }
}

I have Dispatch Class with other sub class, i need to convert XML to Java Object. Code for same:

Public class UnmarshalExample {
    public static void main(String[] args) {
        String xmlString = "<ns1:dispatch xmlns:ns1=\"http://service.order.com\"><ns1:dispatchId>1</ns1:dispatchId><ns1:order><ns1:totalAmount>1000.0</ns1:totalAmount></ns1:order></ns1:dispatch>";

        Dispatch dispatch = (Dispatch) JAXB.unmarshal(
                new StringReader(xmlString), Dispatch.class);

        System.out.println(dispatch);
    }

}

As a output it will return null.

Can any one tell me whats wrong thing in my code?


Solution

  • That is odd, I pasted you code and ran and it produces dispatchId 1:

    Dispatch class:

    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Dispatch implements java.io.Serializable {
       private static final long serialVersionUID = 1L;
        private Integer dispatchId;
    
       /**
        * @return the dispatchId
        */
       public Integer getDispatchId() {
           return dispatchId;
       }
    
       /**
        * @param dispatchId
        *            the dispatchId to set
        */
       public void setDispatchId(Integer dispatchId) {
           this.dispatchId = dispatchId;
       }
    
       @Override
       public String toString() {
           // TODO Auto-generated method stub
           return ""+this.dispatchId;
       }
    }
    

    Test class

    import java.io.*;
    import javax.xml.bind.JAXB;
    
    public class Test {
    
      public static void main(String[] args) {
        String xmlString = "<ns1:dispatch xmlns:ns1=\"http://service.order.com\"><ns1:dispatchId>1</ns1:dispatchId><ns1:order><ns1:totalAmount>1000.0</ns1:totalAmount></ns1:order></ns1:dispatch>";
    
        Dispatch dispatch = (Dispatch) JAXB.unmarshal(
                new StringReader(xmlString), Dispatch.class);
    
        System.out.println(dispatch);
      }
    }
    

    output is

    1