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

问题描述 投票:0回答:2

我正在尝试将xml映射到java类。Xml来自第三方服务。结构相同,但是前缀或名称空间可能不同。 XML:

<?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();
    }

我收到错误:

javax.xml.bind.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.reportError(Loader.java:247)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext $ DefaultRootLoader.childElement(UnmarshallingContext.java:1131)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)在com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)在com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)在com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl $ NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)在com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:852)

java xml jaxb mapping
2个回答
0
投票
类似的东西:

@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; }

您可能还需要向Requisites中的每个元素添加名称空间。


0
投票
@XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")

如果仍然抛出相同的错误,则还必须在@XmlElement中指定名称空间属性:

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

您的Requisites标记内的每个属性还应在其@XmlElement批注中包含名称空间。

© www.soinside.com 2019 - 2024. All rights reserved.