2013-02-15 70 views
0

我通常會得到這樣的錯誤不是當上線:沒有聲明可以爲元素(XML架構)中找到

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'. 

什麼解決的辦法,使其不必連接到互聯網只是爲了這個每一次。

回答

3

你不會在你的問題中確切地說明你是如何加載需要這個模式的XML文件的,但是它的名字看起來與Spring bean配置有關。 Spring爲組件提供了一種機制,這些組件提供了自己的模式來將這些模式捆綁到他們的JAR文件中,所以他們不需要從互聯網中獲取它們。這涉及命名META-INF/spring.schemas在JAR文件中的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目錄中列出,因此它必須到互聯網上下載它。例如,如果您有hazelcast-spring-2.5.jar,則需要使用xsi:schemaLocation中的匹配http://www.hazelcast.com/schema/spring/hazelcast-spring-2.5.xsd

+0

我明白了,那可能是問題所在 – xybrek 2013-02-15 17:55:44

1

下載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); 
0

複製的XSD XSD到WEB-INF /並給予URI作爲/WEB-INF/xsdname的.xsd中,你是在XML文件中使用模式。

相關問題