Jaxb解组返回null对象

问题描述

我有一个非常有趣的xml,我正在尝试将此xml转换为pojo。在我的jaxb解组其返回的空对象之后。

这是xml:

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

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlRootElement(name = "ServiceResultOfarrayofstringuHEDJ7Dj",namespace = "http://schemas.datacontract.org/2004/07/HotelWeb.SanAdminSite.RestWebApi.App_Data")
public class PostSendLogIdServiceXmlResult {

    private String xmlns;

    @XmlElement(name = "InfoMessage")
    private String infoMessage;

    @XmlElement(name = "Error")
    private Error error;

    @XmlElement(name = "Data")
    private PostSendLogIdData data;

    @XmlElement(name = "IsSuccessful")
    private boolean isSuccessful;

    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }

    public String getInfoMessage() {
        return infoMessage;
    }

    public void setInfoMessage(String infoMessage) {
        this.infoMessage = infoMessage;
    }

    public Error getError() {
        return error;
    }

    public void setError(Error error) {
        this.error = error;
    }

    public PostSendLogIdData getData() {
        return data;
    }

    public void setData(PostSendLogIdData data) {
        this.data = data;
    }

    public boolean isSuccessful() {
        return isSuccessful;
    }

    public void setSuccessful(boolean successful) {
        isSuccessful = successful;
    }

    @Override
    public String toString() {
        return "PostSendLogIdServiceXmlResult{" +
                "xmlns='" + xmlns + '\'' +
                ",infoMessage='" + infoMessage + '\'' +
                ",error=" + error +
                ",data=" + data +
                ",isSuccessful=" + isSuccessful +
                '}';
    }
}

我的POJO:

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 = "Error")
@XmlAccessorType(XmlAccesstype.FIELD)
public class PostSendLogIdError {

    @XmlElement(name = "Code")
    private String code;
    @XmlElement(name = "Message")
    private String message;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
import javax.xml.bind.annotation.XmlAccesstype;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Arrays;

@XmlRootElement(name = "Data",namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")
@XmlAccessorType(XmlAccesstype.FIELD)
public class PostSendLogIdData {

    @XmlElement(name = "string")
    private String[] postSendLogIds;

    private String _xmlns;

    public String[] getPostSendLogIds() {
        return postSendLogIds;
    }

    public void setPostSendLogIds(String[] postSendLogIds) {
        this.postSendLogIds = postSendLogIds;
    }

    public String get_xmlns() {
        return _xmlns;
    }

    public void set_xmlns(String _xmlns) {
        this._xmlns = _xmlns;
    }

    @Override
    public String toString() {
        return "PostSendLogIdData{" +
                "postSendLogIds=" + Arrays.toString(postSendLogIds) +
                ",_xmlns='" + _xmlns + '\'' +
                '}';
    }
}
 context = JAXBContext.newInstance(PostSendLogIdServiceXmlResult.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
 PostSendLogIdServiceXmlResult unmarshal = (PostSendLogIdServiceXmlResult) unmarshaller.unmarshal(new StringReader(response));

我的Jaxb转换实现:

PostSendLogIdServiceXmlResult

此后,我得到一个新的 int minSum(vector<int>v,int k){ // in time O(n+10000) vector<int>C(10001,0); for(int x:v)C[x]++; int ans=0; for(int i=10000;i>=1;i--){ int count=min(C[i],k); C[i]-=count; C[(i+1)/2]+=count; k-=count; ans+=i*C[i]; } return ans; } int main(void){ cout<<minSum({10,20,7},4)<<endl; cout<<minSum({1,2},1)<<endl; cout<<minSum({1,2,1},2)<<endl; return 0; } // output 14,3 对象,该对象的值为空。

能给我提些建议吗?

预先感谢。

解决方法

再次查看XML。命名空间不应用于<Data>元素,而是应用于<string>子元素。注意d2p1:前缀的应用位置。

此外,<Data>不是根元素,因此整个@XmlRootElement(name = "Data",...注释都未使用,即是不必要的,并且似乎使您感到困惑,因此我建议从{{ 1}}类。

正确使用是:

PostSendLogIdData