Java 找不到元素(XML架构)的声明

Java 找不到元素(XML架构)的声明,java,xml,eclipse,schema,Java,Xml,Eclipse,Schema,我不在线时通常会遇到这样的错误: org.xml.sax.SAXParseException; lineNumber: 55; columnNumber: 33; schema_reference.4: Failed to read schema document 'http://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd', because 1) could not find the document; 2) the do

我不在线时通常会遇到这样的错误:

org.xml.sax.SAXParseException; lineNumber: 55; columnNumber: 33; schema_reference.4: Failed to read schema document 'http://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd', 
because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

Caused by: java.net.ConnectException: Connection timed out: connect

...no declaration can be found for element 'hz:hazelcast'.
org.xml.sax.SAXParseException;行号:55 ;;栏目号:33;架构\u引用。4:未能读取架构文档'http://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd', 
因为1)找不到文档;2) 文件无法读取;3) 文档的根元素不是。
原因:java.net.ConnectException:连接超时:连接
…找不到元素“hz:hazelcast”的声明。

解决方案是什么,这样它就不必每次都连接到互联网

下载xsd并将其保存在项目中。使用下面的代码访问xsd

JAXBContext context = JAXBContext.newInstance(<your>.class);
Unmarshaller jaxbUnMarshaller = context.createUnmarshaller();


SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL tmpurl = getClass().getClassLoader().getResource("file.xsd");
Schema s = schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);
JAXBContext context=JAXBContext.newInstance(.class);
Unmarshaller=context.createUnmarshaller();
SchemaFactory SchemaFactory=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI);
URL tmpurl=getClass().getClassLoader().getResource(“file.xsd”);
Schema s=schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);

下载xsd并将其保存在项目中。使用下面的代码访问xsd

JAXBContext context = JAXBContext.newInstance(<your>.class);
Unmarshaller jaxbUnMarshaller = context.createUnmarshaller();


SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL tmpurl = getClass().getClassLoader().getResource("file.xsd");
Schema s = schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);
JAXBContext context=JAXBContext.newInstance(.class);
Unmarshaller=context.createUnmarshaller();
SchemaFactory SchemaFactory=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI);
URL tmpurl=getClass().getClassLoader().getResource(“file.xsd”);
Schema s=schemaFactory.newSchema(tmpurl);
jaxbUnMarshaller.setSchema(s);

将该XSD复制到WEB-INF/中,并将uri设置为/WEB-INF/xsdname.XSD放在使用架构的XML文件中。

将该XSD复制到WEB-INF/中,并在使用架构的XML文件中将uri设置为/WEB-INF/XSD您的问题是如何加载需要此模式的XML文件,但就其名称而言,它似乎与Springbean配置有关。Spring提供了一种机制,让提供自己模式的组件将这些模式捆绑到JAR文件中,这样就不必从internet获取这些模式。这涉及JAR文件中名为
META-INF/spring.schemas
java.util.Properties
格式文件,例如,该文件包含将http URL映射到本地路径(JAR文件内)的行

http\://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd=hazelcast-spring-2.1.xsd
(摘自hazelcast-spring-2.1.3.jar)


因此,我怀疑这里发生的情况是,您所指的架构版本与您实际使用的hazelcast版本不同,这意味着您请求的架构没有列在
spring.schemas
目录中,因此它必须到internet下载。例如,如果您有
hazelcast-spring-2.5.jar
,则需要使用匹配的
http://www.hazelcast.com/schema/spring/hazelcast-spring-2.5.xsd
xsi:schemaLocation

中,您没有在问题中明确说明如何加载需要此架构的XML文件,但它的名字似乎与Springbean配置有关。Spring提供了一种机制,让提供自己模式的组件将这些模式捆绑到JAR文件中,这样就不必从internet获取这些模式。这涉及JAR文件中名为
META-INF/spring.schemas
java.util.Properties
格式文件,例如,该文件包含将http URL映射到本地路径(JAR文件内)的行

http\://www.hazelcast.com/schema/spring/hazelcast-spring-2.1.xsd=hazelcast-spring-2.1.xsd
(摘自hazelcast-spring-2.1.3.jar)


因此,我怀疑这里发生的情况是,您所指的架构版本与您实际使用的hazelcast版本不同,这意味着您请求的架构没有列在
spring.schemas
目录中,因此它必须到internet下载。例如,如果您有
hazelcast-spring-2.5.jar
,则需要使用匹配的
http://www.hazelcast.com/schema/spring/hazelcast-spring-2.5.xsd
xsi:schemaLocation

中,我明白了,这可能是我所看到的问题,也可能是问题所在