問題描述
我有兩個架構(gòu)文件,一個是從另一個導(dǎo)入的.在 Eclipse 架構(gòu)中執(zhí)行代碼時找到但從 jar 架構(gòu)文件中執(zhí)行代碼時找不到
I have two schema files one is imported from the other. When executing the code in Eclipse schemas are found but when executing the code from jar schema files are not found
這里是代碼
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
SchemaFactory schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
try {
factory.setSchema(schemaFactory.newSchema(new Source[] {
new StreamSource(getClass().getResource("Liso.xsd")
.getFile()),
new StreamSource(getClass().getResource("LisoXml.xsd")
.getFile()) }));
this.saxParser = factory.newSAXParser();
} catch (SAXException se) {
System.out.println("SCHEMA : " + se.getMessage()); // problem in the XSD itself
}
這是我得到的錯誤
SCHEMA : schema_reference.4: Failed to read schema document 'file:/C:/Tools/lib/LisoTools.jar!/com/xerox/liso/xml/Liso.xsd', because 1) could not find the do
cument; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
謝謝
推薦答案
如果Liso.xsd
是在導(dǎo)入LisoXml.xsd
,那么定義即可>Liso.xsd
到模式工廠,如下所示.該 API 足夠智能,可以加載導(dǎo)入/包含的架構(gòu).
If Liso.xsd
is importing LisoXml.xsd
, then it is just sufficient to define Liso.xsd
to the schema factory as shown below. The api is smart enough the load the imported/included schema(s).
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema")
Schema compiledSchema = schemaFactory.newSchema(getClass().getClassLoader().getResource("Liso.xsd"))
我驗證這在 1.5 和 1.6 上都有效.在 1.6 上,您可能會遇到 這個問題如果使用 DOM 就好了.
I verified this worked on both 1.5 and 1.6. On 1.6, you might hit in to this issue as well if using DOM.
這篇關(guān)于在 jar 文件中引用 XSD 架構(gòu)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!