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

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

葛炜
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”,本地:“container”)。需要的元素为<{}Container>,<{}Requisite>(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingcontext.handleEvent(unmarshallingcontext.java:726)(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.reporterror(loader.java:247)(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.reporterror(loader.java:242)(位于ment(AbstractSAXParser.java:509)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.ScanStartElement(XmlnsDocumentScannerImpl.java:374)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.java:374)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.ScanRotelEmentHook(

UPD:我添加了名称空间,错误就消失了。但我的对象的字段中没有填充信息。它们是空的,尽管在xml中它们填充了信息

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

共有1个答案

萧阳波
2023-03-14
@XmlRootElement(name = "container", namespace = "http://www.namespace.com/RTS")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {

    @XmlElement(name = "requisites", namespace="http://www.namespace.com/RTS")
    private Requisites requisites;

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

    @XmlElement(name = "documentKind", namespace="http://www.namespace.com/RTS")
    private String documentKind;
    @XmlElement(name = "classification", namespace="http://www.namespace.com/RTS")
    private String classification;
    @XmlElement(name = "annotation", namespace="http://www.namespace.com/RTS")
    private String annotation;
 类似资料: