javax.xml.bind.UnmarshalException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15751632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:57:32  来源:igfitidea点击:

javax.xml.bind.UnmarshalException

xmlnamespacesjaxbunmarshalling

提问by dreambigcoder

I am getting the following error:

我收到以下错误:

javax.xml.bind.UnmarshalException: unexpected element(uri:"http://www.docsite.com/ClientConfig.xsd", local:"ClientConfig").
Expected elements are <{http://www.docsite.com/ClientConfig.xsd/}ClientConfig>

my root element class file is:

我的根元素类文件是:

@XmlRootElement(name="ClientConfig",namespace="http://www.docsite.com/ClientConfig.xsd/")
public class ClientConfig {}

my package.info file is:

我的 package.info 文件是:

@XmlSchema(namespace="http://www.docsite.com/ClientConfig.xsd",elementFormDefault=XmlNsForm.QUALIFIED)

package com.convertXml.docSite.XmlConverter;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

let me know what can I do to fix this

让我知道我能做些什么来解决这个问题

采纳答案by bdoughan

TL;DR

TL; 博士

You have an extra / at the end of the namespace specified in the @XmlRootElementannotation.

@XmlRootElement注释中指定的命名空间末尾有一个额外的 / 。



LONG ANSWER

长答案

package-info

包信息

The namespace is specified correctly in the package level @XmlSchemaannotation:

在包级别@XmlSchema注释中正确指定了命名空间:

@XmlSchema(namespace="http://www.docsite.com/ClientConfig.xsd",elementFormDefault=XmlNsForm.QUALIFIED)
package com.convertXml.docSite.XmlConverter;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

ClientConfig

客户端配置

But you have overridden it with an incorrect namespace on the ClientConfigclass. You have an extra /at the end of the namespace specified in the @XmlRooElementannotation.

但是您在ClientConfig类上使用不正确的命名空间覆盖了它。/@XmlRooElement注释中指定的命名空间的末尾有一个额外的。

package com.convertXml.docSite.XmlConverter;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="ClientConfig",namespace="http://www.docsite.com/ClientConfig.xsd/")
public class ClientConfig {}

Since you declared the namespace on the @XmlSchemaon the package-infoclass you don't need to repeat it on the @XmlRootElement.

由于您@XmlSchemapackage-info类的上声明了命名空间,因此您无需在@XmlRootElement.

package com.convertXml.docSite.XmlConverter;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="ClientConfig")
public class ClientConfig {}

Demo

演示

Now the unmarshalwill work correctly:

现在unmarshal将正常工作:

package com.convertXml.docSite.XmlConverter;

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(ClientConfig.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader xml = new StringReader("<ClientConfig xmlns='http://www.docsite.com/ClientConfig.xsd'/>");
        ClientConfig clientConfig = (ClientConfig) unmarshaller.unmarshal(xml);
    }

}

For More Information

想要查询更多的信息