XML到JavaBean

时间:2014-07-30 15:00:20

标签: java xml

XML文件如下:

<AuthnRequest xmlns="A" ID="B">
    <Issuer xmlns="A">SOMETHING</Issuer>
    <NameIDPolicy Format="B" AllowCreate="true"/>
</AuthnRequest>

那么如何将具有许多属性的文件转换为Javabean?

我这样累了,但没有工作

@XmlRootElement(name="AuthnRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserAuthn {

    @XmlAttribute
    private String xmlns;
    @XmlAttribute   
    private String ID;
    @XmlAttribute
    private String Version;
    @XmlAttribute
    private String IssueInstant;
    @XmlElement
    private List<Issuer> Issuers;
    @XmlElement
    private List<NameIDPolicy> NameIDPolicys;

    // Getters and Setters
}

班级发行人

private String xmlns;

@XmlAttribute
public String getXmlns() {
    return xmlns;
}

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

类NameIDPolicy是相同的:

private String Format;
private String AllowCreate;

@XmlAttribute(name="AllowCreate")
public String getFormat() {
    return Format;
}

public void setFormat(String format) {
    Format = format;
}
@XmlAttribute(name="Format")
public String getAllowCreate() {
    return AllowCreate;
}

public void setAllowCreate(String allowCreate) {
    AllowCreate = allowCreate;
}

错误是

threw exception [Request processing failed; nested exception is 
javax.xml.bind.UnmarshalException: unexpected element (uri:"A", local:"AuthnRequest"). 
Expected elements are <{}AuthnRequest>] with root cause

1 个答案:

答案 0 :(得分:0)

错误消息告诉您<AuthnRequest>元素位于命名空间A中,并且您的注释不指定该命名空间。该命名空间在特殊的xmlns属性中指定,您尝试使用@XmlAttribute

进行映射

试试这个:

@XmlRootElement(name="AuthnRequest", namespace="A")

并删除@XmlAttribute的{​​{1}}。

您可能还需要为xmlns中的某些namespace个带注释的字段指定@XmlElement,例如

UserAuthn
相关问题