UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container")

huangapple go评论76阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xdms:container xmlns:xdms=&quot;http://www.namespace.com/RTS&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xdms:uid=&quot;FHGHDFGDFJKGDFHG&quot; xdms:version=&quot;3.2&quot;&gt;
    &lt;xdms:requisites&gt;
        &lt;xdms:documentKind&gt;letter&lt;/xdms:documentKind&gt;
        &lt;xdms:classification&gt;main&lt;/xdms:classification&gt;
        &lt;xdms:annotation&gt;unknown&lt;/xdms:annotation&gt;
    &lt;/xdms:requisites&gt;
&lt;/&gt;

My classes:

@XmlRootElement(name = &quot;container&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
    private static final long serialVersionUID = 1L;

    @XmlElement(name = &quot;requisites&quot;)
    private Requisites requisites;

    public Container() {
        super();
    }

    @Override
    public String toString() {
        return &quot;Container{&quot; +
                &quot;requisites=&quot; + requisites +
                &#39;}&#39;;
    }
}

@XmlRootElement(name = &quot;requisites&quot;)
@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 &quot;Requisites{&quot; +
                &quot;documentKind=&#39;&quot; + documentKind + &#39;\&#39;&#39; +
                &quot;, classfication=&#39;&quot; + classification + &#39;\&#39;&#39; +
                &quot;, annotation=&#39;&quot; + annotation + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
}

and the main class in which I run parsing:

    JAXBContext jaxbContext;
    File xmlFile = new File(&quot;test.xml&quot;);
    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 = &quot;container&quot;, namespace = &quot;http://www.namespace.com/RTS&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {

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

}

you might need to add namespace to every element in Requisites as well.

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

    @XmlElement(name = &quot;documentKind&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
    private String documentKind;
    @XmlElement(name = &quot;classification&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
    private String classification;
    @XmlElement(name = &quot;annotation&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
    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=&quot;container&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)

In case, if it still throws the same error, you also have to specify the namespace attribute in your @XmlElement:

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

Your each attribute inside the Requisites tag should also contain the namespace within its @XmlElement annotation.

huangapple
  • 本文由 发表于 2020年4月6日 20:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定