当前位置: 首页 > 知识库问答 >
问题:

UnmarshalException:意外元素(URI:“http://www.namespace.com/rts”,本地:“容器”)

徐博雅
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<xdms:container xmlns:xdms="http://www.namespace.com/RTS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xdms:uid="FHGHDFGDFJKGDFHG" xdms:version="3.2">
    <xdms:requisites>
        <xdms:documentKind>letter</xdms:documentKind>
        <xdms:classification>main</xdms:classification>
        <xdms:annotation>unknown</xdms:annotation>
    </xdms:requisites>
</>
@XmlRootElement(name = "container")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
    private static final long serialVersionUID = 1L;

    @XmlElement(name = "requisites")
    private Requisites requisites;

    public Container() {
        super();
    }

    @Override
    public String toString() {
        return "Container{" +
                "requisites=" + requisites +
                '}';
    }
}

@XmlRootElement(name = "requisites")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requisites implements Serializable {
    private static final long serialVersionUID = 1L;

    private String documentKind;
    private String classification;
    private String annotation;

    public Requisites() {
        super();
    }

    @Override
    public String toString() {
        return "Requisites{" +
                "documentKind='" + documentKind + '\'' +
                ", classfication='" + classification + '\'' +
                ", annotation='" + annotation + '\'' +
                '}';
    }
}
    JAXBContext jaxbContext;
    File xmlFile = new File("test.xml");
    try
    {
        jaxbContext = JAXBContext.newInstance(Container.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Container cont = (Container) jaxbUnmarshaller.unmarshal(xmlFile);

        System.out.println(cont);
    }
    catch (JAXBException e)
    {
        e.printStackTrace();
    }

unmarshalException:意外元素(URI:“http://www.namespace.com/rts”,本地:“容器”)。在com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingContext.HandleEvent(unmarshallingContext.java:726)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.Reporder(loader.java:247)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.Reporder(loader.java:242)在)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingcontext$defaultRootLoader.childelement(UnmarshallingContext.java:1131)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.v2.runtime.unmarshallingContext._startelement(UnmarshallingContext.java:556)在在com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.startelement(saxconnector.java:153)在com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.scanstartelement(xmlnsdocumentscannerimpl.java:374)在com.sun.org.apache.aperce.xerces.internal.impl.xmlnsdocumentscannerimpl.java.$nscontentdriver.scanrootelementhook(xmlnsdocumentscannerimpl.java:613)在com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl$prologdriver.next(xmldocumentscannerimpl.java:852)

UPD:我html" target="_blank">添加了命名空间,错误消失了。但是我的对象的字段没有填充信息。它们是空的,尽管在xml中它们填充了信息

容器{requisites=requisites{documentkind='null',classfication='null',annotation='null'}}

共有1个答案

杨腾
2023-03-14
@XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")
@XmlElement(name="requisites", namespace="http://www.namespace.com/RTS")
 类似资料: