java 异常 cvc-elt.1:尝试使用 JAXB 解组时找不到元素“AsifXml”的声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4453756/
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
Exception cvc-elt.1: Cannot find the declaration of element 'AsifXml' when trying to unmarshall using JAXB
提问by Mike Smith
I keep receiving the following error when attempting to unmarshall an xml document using JAXB. The error reads as follows:
尝试使用 JAXB 解组 xml 文档时,我不断收到以下错误。错误如下:
cvc-elt.1: Cannot find the declaration of element 'AsifXml'
cvc-elt.1:找不到元素“AsifXml”的声明
Code to unmarshall is this:
解组的代码是这样的:
JAXBContext jc = JAXBContext.newInstance("asif_objects");
Unmarshaller u = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("ASIF_Schema1.1.6.xsd"));
u.setSchema(schema);
AsifXml doc
= (AsifXml)u.unmarshal(new FileInputStream("asif_small.xml"));
I believe the relevant part of the xsd files looks like this(omitted opening and closing of tags as I'm not sure how to format them on the website:
我相信 xsd 文件的相关部分看起来像这样(省略了标签的打开和关闭,因为我不确定如何在网站上设置它们的格式:
?xml version="1.0" encoding="UTF-8"?
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:asif="http://www.website.com/ASIF"
targetNamespace="http://www.website.com/ASIF" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.1.6"
xs:element name="AsifXml"
xs:complexType
And what I believe to be the relevant XML is here:
我认为相关的 XML 在这里:
AsifXml xmlns:AsifXml="http://www.website.com/ASIF"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.4"
content="study"
So far I've tried googling for this same error, but it seems most responses have to do with errors in web services which I'm not using. I also thought that maybe I was having problems validating because I am behind a proxy and it was unable to reach the url's given in the schema documents, but upon further research I'm getting the impression that those url's don't actually have to exist to validate the document. And I tried on another network that isn't behind a proxy and received the same error.
到目前为止,我已经尝试在谷歌上搜索同样的错误,但似乎大多数响应都与我没有使用的 Web 服务中的错误有关。我还想,也许我在验证时遇到了问题,因为我在代理后面并且无法访问架构文档中给出的 url,但经过进一步研究,我得到的印象是这些 url 实际上不必存在来验证文件。我在另一个不在代理后面的网络上尝试并收到相同的错误。
Any help with this problem is greatly appreciated.
非常感谢您对这个问题的任何帮助。
采纳答案by skaffman
The sample XML file looks bogus to me. It looks fine at first glance, but it makes no sense:
示例 XML 文件在我看来是假的。乍一看还不错,但是没有意义:
<AsifXml xmlns:AsifXml="http://www.website.com/ASIF">
This declares a tag called AsifXml
, and declares an XML namespace with a prefix also called AsifXml
. However, the two have nothing to do with each other. The element itself is left without any namespace, which violates the schema.
这声明了一个名为 的标记AsifXml
,并声明了一个前缀也称为 的 XML 命名空间AsifXml
。然而,两者之间没有任何关系。元素本身没有任何命名空间,这违反了架构。
Try the following sample instead, it should work:
请改为尝试以下示例,它应该可以工作:
<AsifXml xmlns="http://www.website.com/ASIF"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.1.4"
content="study">
This sample say that the AsifXml
element has the namespace http://www.website.com/ASIF
, which is what the schema expects.
此示例说明该AsifXml
元素具有 namespace http://www.website.com/ASIF
,这是架构所期望的。
That sample could be equivalently encoded as:
该样本可以等效地编码为:
<AsifXml:AsifXml xmlns:AsifXml="http://www.website.com/ASIF"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.1.4"
content="study">
It amounts to the same thing, but this version is confusing and verbose.
它相当于同一件事,但这个版本令人困惑和冗长。