简体   繁体   中英

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?

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

The order is of the type Order. We don't see any code of this class. This is probably the cause of it as well. You may not provide decent data for the Order to be constructed.

To see how namespaces are used: http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM