通过jar文件进行SAX解析声明XML文档无效 [英] SAX parsing through a jar file declares XML document invalid
问题描述
图片不言自明.当我在CMD中使用相同的过程验证文档时,该文档被声明为有效.手动检查时有效.
The picture is self explanatory. When I validate the document using identical process in CMD, the document is declared valid. It is valid when checked manually.
jar文件生成以下消息:
The jar file generates this message:
Invalid
schema_reference.4: Failed to read schema document 'E:/XML and Java/Pro XML Development with Java/schema.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>.
问题是:为什么?
我正在制作的应用程序需要使用XSD通过JAR文件验证XML的过程.
The process of validating an XML using XSD via a JAR file is necessary for the application I am making.
package xml;
import javax.swing.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
public class JarValidator extends JFrame{
JTextArea area = new JTextArea();
JScrollPane pane = new JScrollPane(area);
SAXParserFactory factory;
SAXParser parser;
final String USER_DIR = System.getProperty("user.dir").replace('\\','/').concat("/");
final String XML_FILE_NAME = "helloWorld.xml";
final String SCHEMA_FILE_NAME = "schema.xsd";
final String JAXP_SCHEMA_LANGUAGE_PROPERTY =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
final String W3_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
final String JAXP_SCHEMA_SOURCE_PROPERTY = "http://java.sun.com/xml/jaxp/properties/schemaSource";
public JarValidator(){
this.getContentPane().add(pane);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
try{
factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
parser = factory.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE_PROPERTY, W3_SCHEMA_LANGUAGE);
parser.setProperty(JAXP_SCHEMA_SOURCE_PROPERTY,
USER_DIR+SCHEMA_FILE_NAME);
ErrorHandler handler = new ErrorHandler();
parser.parse(new java.io.File(USER_DIR+XML_FILE_NAME), handler);
if(handler.errorOccured == true){
area.append("Invalid \n");
area.append(handler.ex.getMessage());
}else{
area.append("Valid");
}
}catch(SAXException e){
area.append(e.getMessage());
}catch(ParserConfigurationException e){
area.append(e.getMessage());
}catch(java.io.IOException e){
area.append(e.getMessage());
}catch(Exception e){
area.append(e.getMessage());
}
}
private class ErrorHandler extends DefaultHandler{
public SAXParseException ex = null;
public boolean errorOccured = false;
@Override
public void error(SAXParseException ex) {
this.ex = ex;
errorOccured = true;
}
@Override
public void fatalError(SAXParseException ex){
this.ex = ex;
errorOccured = true;
}
@Override
public void warning(SAXParseException ex){
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override public void run(){
new JarValidator();
}
});
}
}
在任何文件夹名称中都没有空格的目录结构上,出现相同的错误.
On a directory structure with no spaces in the name of any folders, the same error.
Invalid
schema_reference.4: Failed to read schema document 'E:/test/schema.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>.
我以为我可以添加:
我在Windows 7 32位计算机上.
该JAR与XML和XSD位于同一文件中,因此 user.dir
可以正常工作.
Just thought I might add:
I am on a Windows 7 32 bit machine.
The JAR is in the same file as the XML and XSD hence user.dir
works.
推荐答案
在Java XML世界中, String
参数很少(也许永远不会)引用文件名.相反,通常期望它包含一个URL.鉴于此,我将替换以下代码:
In the Java XML world, a String
parameter rarely (perhaps never) refers to a filename. Instead, it usually is expected to contain a URL. Given that, I would replace this code:
parser.setProperty(JAXP_SCHEMA_SOURCE_PROPERTY, USER_DIR+SCHEMA_FILE_NAME);
通过以下代码(这也避免了分隔符的破坏和字符串连接):
By this code (which also avoids the separator munging and string concatenation):
File userDir = new File(System.getProperty("user.dir"));
File schemaFile = new File(userDir, SCHEMA_FILE_NAME);
parser.setProperty(JAXP_SCHEMA_SOURCE_PROPERTY, schemaFile);
两个警告:
- 我只是键入了这个;它可能包含错别字.
- 我没有花时间检查财产的文件.但是,这就是所有其他JDK XML API的工作方式,因此我确信它是正确的.无论您使用什么文档来获取属性名称,都应该为您提供更多信息.
这篇关于通过jar文件进行SAX解析声明XML文档无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!