gpt4 book ai didi

xml - javax.xml.bind.UnmarshalException

转载 作者:数据小太阳 更新时间:2023-10-29 01:51:23 24 4
gpt4 key购买 nike

我收到以下错误:

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>

我的根元素类文件是:

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

我的 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;

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

最佳答案

长话短说

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


长答案

包信息

命名空间在包级@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 类上用不正确的命名空间覆盖了它。在 @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 {}

由于您在 package-info 类的 @XmlSchema 上声明了命名空间,因此您不需要在 @XmlRootElement 上重复它>.

package com.convertXml.docSite.XmlConverter;

import javax.xml.bind.annotation.XmlRootElement;

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

演示

现在 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);
}

}

了解更多信息

关于xml - javax.xml.bind.UnmarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com