使用Jaxb2解组复杂类型

时间:2011-09-23 21:49:24

标签: java xml jaxb unmarshalling jaxb2

当我使用Spring OXM框架解组使用JAXB2处理器时,我有以下XML和java类我得到了所有空值。有什么想法吗?

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

@XmlRootElement(name="AuthentXML",namespace="http://xml.authentify.net/MessageSchema.xml")
public class AuthentifyResult { 
    private Header header;
    public Header getHeader() {
        return header;
    }
    public void setHeader(Header header) {
        this.header = header;
    }
}

@XmlType(name="", propOrder={"asid", "teid", "replyTo"}) 
public class Header {
 private String asid;

private String teid;

private String replyTo;
public String getAsid() {
 return asid;
}
public void setAsid(String asid) {
this.asid = asid;
}
public String getTeid() {
return teid;
}
public void setTeid(String teid) {
this.teid = teid;
}
public String getReplyTo() {
return replyTo;
}
public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
}

<?xml version="1.0" ?>
<AuthentXML>
<header>
<asid>AuthenticationSubjectID</asid>
<teid>B6F997AE-FB4E-11D3-80BD-0050DA5DC7B8</teid>
<replyTo>https://r1.authentify.net/s2s/default.asp</replyTo>
</header>
</AuthentXML>

unmarshaling code

FileInputStream is = null;

is = new FileInputStream(FILE_NAME);
this.authentifyResult = (AuthentifyResult) this.jaxbUnmarshaller.unmarshal(new StreamSource(is));

我在authentifyResult中收到了标头null。为什么呢?

1 个答案:

答案 0 :(得分:2)

我唯一看错的代码是@XmlRootElement上的AuthentifyResult注释不应指定命名空间,因为您发布的XML片段不是命名空间限定的:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="AuthentXML")
public class AuthentifyResult { 
    private Header header;
    public Header getHeader() {
        return header;
    }
    public void setHeader(Header header) {
        this.header = header;
    }
}