英文:
UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container")
问题
我正在尝试将我的 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>
</xdms:container>
我的类:
@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 + '\'' +
                ", classification='" + 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: unexpected element
(uri:"http://www.namespace.com/RTS", local:"container"). Expected
elements are <{}container>,<{}requisites> 	at
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
...
更新:
我添加了命名空间,错误消失了。但是我的对象字段没有填充信息。它们是空的,尽管在 XML 中它们填充有信息。
Container{requisites=Requisites{documentKind='null',
classfication='null', annotation='null'}}
英文:
I am trying to map my xml to java classes.
Xml comes from a third-party service. The structure is the same, but there may be a different prefix or namespace. 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>
</>
My classes:
@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 + '\'' +
                '}';
    }
}
and the main class in which I run parsing:
    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();
    }
And I get the error:
> javax.xml.bind.UnmarshalException: unexpected element
> (uri:"http://www.namespace.com/RTS", local:"container"). Expected
> elements are <{}container>,<{}requisites> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
> 	at
> com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
> 	at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
> 	at
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
> 	at
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
> 	at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
> 	at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
UPD:
I added nameSpace, the error is gone. But the fields of my objects are not filled with information. They are empty, although in xml they are filled with information
> Container{requisites=Requisites{documentKind='null',
> classfication='null', annotation='null'}}
答案1
得分: 1
你没有在你的jaxb类中指定命名空间
类似这样的:
@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中的每个元素添加命名空间。
@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;
}
英文:
You don't specify the namespace in your jaxb class
Something like that:
@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;
}
you might need to add namespace to every element in Requisites as well.
@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;
答案2
得分: 1
在@XmlRootElement中使用namespace属性,如下所示:
@XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")
如果仍然出现相同的错误,您还需要在@XmlElement中指定namespace属性:
@XmlElement(name="requisites", namespace="http://www.namespace.com/RTS")
您在Requisites标签内的每个属性也应在其@XmlElement注释中包含命名空间。
英文:
Use the namespace attribute in the @XmlRootElement as follows:
@XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")
In case, if it still throws the same error, you also have to specify the namespace attribute in your @XmlElement:
@XmlElement(name="requisites", namespace="http://www.namespace.com/RTS")
Your each attribute inside the Requisites tag should also contain the namespace within its @XmlElement annotation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论