通过JAXB在Unmarshal XML中缺少一些值

时间:2014-01-30 09:17:09

标签: java xml jaxb unmarshalling

我有一个XML字符串,我想使用JAXB API解组到Java Bean 以下是XML:

<?xml version="1.0" encoding="UTF-8"?>
  <ns0:TEST_REQ xmlns:ns0="http://Test.com">
    <keys>
       <item>
          <key>MyKey1</key>     
       </item>
       <item>
          <key>MyKey2</key>      
       </item>
    </keys>
    <root>
      <messageid>000000123<messageid/>
      <statusmessage>Successfully Done.</statusmessage>
      <statuscode>1</statuscode>
    </root>
</ns0:TEST_REQ>  

我想将这个XML解组到Java Bean下面:

@XmlRootElement(name = "TEST_REQ", namespace = "http://Test.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResponseKeysBean {

    @XmlElement(name="messageid")
    private String messageId;

    @XmlElement(name="statuscode")
    private Integer statusCode;

    @XmlElement(name="statusmessage")
    private String statusMessage;

    @XmlElement(name="key")
    private String key;

    @XmlElementWrapper(name="keys")
    @XmlElement(name="item")
    private List<ResponseKeysBean> keysList;

    //Getter and Setter method
}  

我使用以下代码解组此XML:

JAXBContext jaxbContext = JAXBContext.newInstance(ResponseKeysBean.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
ResponseKeysBean keyObj = (ResponseKeysBean) um.unmarshal(new StringReader(xmlStr));  

当我在XML上解组时,我获得了keysList对象中的密钥列表,但我没有在我的对象中获取其他参数,如messageId<root>元素下的其他参数。 /> 我怎样才能在bean变量中获得这些值。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

您尚未正确创建ResponseKeysBean课程。这样做:

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "TEST_REQ", namespace = "http://Test.com")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class ResponseKeysBean {

    private Keys keyList;
    private Root status;

    public ResponseKeysBean() {
        // TODO Auto-generated constructor stub
    }

    @XmlElement(name="keys")
    public Keys getKeyList() {
        return keyList;
    }

    public void setKeyList(Keys keyList) {
        this.keyList = keyList;
    }

    @XmlElement(name="root")
    public Root getStatus() {
        return status;
    }

    public void setStatus(Root status) {
        this.status = status;
    }
}  

class Root {
    private String messageId;
    private Integer statusCode;
    private String statusMessage;

    @XmlElement(name="messageid")
    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    @XmlElement(name="statuscode")
    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    @XmlElement(name="statusmessage")
    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    @Override
    public String toString() {
        return "Root [messageId=" + messageId + ", statusCode=" + statusCode
                + ", statusMessage=" + statusMessage + "]";
    }


}

class Keys {
    private List<Item> keysList;

    public Keys() {
        // TODO Auto-generated constructor stub
    }

    @XmlElement(name="item")
    public List<Item> getKeysList() {
        return keysList;
    }

    public void setKeysList(List<Item> keysList) {
        this.keysList = keysList;
    }

    @Override
    public String toString() {
        return "Keys [keysList=" + keysList + "]";
    }   
}

class Item {

    private String key;

    @XmlElement(name="key")
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String toString() {
        return "Item [key=" + key + "]";
    }
}

然后从您的Main类中调用它:

public static void main(String[] args) throws JAXBException, FileNotFoundException{
        JAXBContext jaxbContext = JAXBContext.newInstance(ResponseKeysBean.class);
        Unmarshaller um = jaxbContext.createUnmarshaller();
        ResponseKeysBean keyObj = (ResponseKeysBean) um.unmarshal(new FileReader(new File("./configurationfiles/testconfig.xml")));

        System.out.println(keyObj.getKeyList());
        System.out.println(keyObj.getStatus());

    }

<强>输出

Key List is : Keys [keysList=[Item [key=MyKey1], Item [key=MyKey2]]]
Status is : Root [messageId=000000123, statusCode=1, statusMessage=Successfully Done.]

答案 1 :(得分:1)

这是用于获取所需XML输出的完整ResponseKeyBean类。

@XmlRootElement(name = "TEST_REQ", namespace = "http://Test.com")
@XmlAccessorType(XmlAccessType.FIELD)
public static class ResponseKeysBean {
    static class Root {
        @XmlElement(name="messageid")
        private String messageId;
        @XmlElement(name="statuscode")
        private Integer statusCode;
        @XmlElement(name="statusmessage")
        private String statusMessage;
        public Root() {
        }
    }
    @XmlElement(name="root")
    private Root root;
    @XmlElementWrapper(name="keys")
    @XmlElement(name="key")
    private List<Key> keys;
    @XmlAccessorType(XmlAccessType.FIELD)
    static class Key {
        @XmlElement(name="item")
        private List<String> key = new ArrayList<>();
        public Key() {}
        public Key(String value) {
            this.key.add(value);
        }
    }
    public ResponseKeysBean() {
        this.keys = new ArrayList<>();
        this.root = new Root();
    }
    public ResponseKeysBean(String messageId, Integer statusCode, String statusMessage) {
        this();
        this.root.messageId = messageId;
        this.root.statusCode = statusCode;
        this.root.statusMessage = statusMessage;
    }
    public void addKey(String key) {
        this.keys.add(new Key(key));
    }
}

映射分为两个内部类,一个用于Root,另一个用于Key。

以下是获取输出的主要方法:

public static void main(String[] args) throws Exception {
    ResponseKeysBean bean = new ResponseKeysBean("000000123",1,"Successfully Done.");
    bean.addKey("MyKey1");
    bean.addKey("MyKey2");
    JAXBContext jaxbContext = JAXBContext.newInstance(ResponseKeysBean.class);
    Unmarshaller um = jaxbContext.createUnmarshaller();
    Marshaller ma = jaxbContext.createMarshaller();
    StringWriter result = new StringWriter();
    ma.marshal(bean, result);
    System.out.println(result.toString());
}

输出是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:TEST_REQ xmlns:ns2="http://Test.com"><root><messageid>000000123</messageid><statuscode>1</statuscode><statusmessage>Successfully Done.</statusmessage></root><keys><key><item>MyKey1</item></key><key><item>MyKey2</item></key></keys></ns2:TEST_REQ>