你这里有一点错误
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
new File("myschema.xsd"));应该把new File("myschema.xsd")换成方法参数schema。另外,不知道你的ErrorHandle是怎么写的。我自己换成ErrorHandler的实现类,结果是可以报错。
解决方案 »
- 求助关于struts零配置和spring整合的问题
- struts2文件上传 为什么文件打开状态中不能上传呢
- 哪里有开源的项目可以看源代码
- spring的配置文件的问题
- hibernate的.lang.NullPointerException错误
- 求助:weblogic9.2,在myeclipse5.5上deploy Enterprise Application Project失败!!
- 怎样得到未读邮件的个数!我只要得到未读邮件的个数就行了!怎么弄!
- hibernate报异常?
- 为什么我的ResultSet不能next()
- JSTL问题
- 求改Struts系统
- 有关weblogic下log4j的配置,等待中!!!
临时整理的,有点乱public class XMLResource {
static public boolean validate(InputStream in, InputStream schema)
throws Exception { //log.debug("DefaultParser.validate() begin:");
boolean result = false;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",new File("myschema.xsd"));
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",schema);
DocumentBuilder parser = factory.newDocumentBuilder(); //parser.setErrorHandler(errorHandle);
parser.setErrorHandler(
new org.xml.sax.ErrorHandler() {
public void fatalError(SAXParseException spe)
throws SAXException {
System.out.println("** Warning"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage()); throw spe;
}
// treat validation errors as fatal
public void error(SAXParseException spe)
throws SAXParseException {
System.out.println("** Warning"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage()); throw spe;
} // dump warnings too
public void warning(SAXParseException err)
throws SAXParseException {
System.out.println("** Warning"
+ ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
}); parser.parse(in); //if (errorHandle.getMsgList().size() == 0) {
result = true;
//}
//log.debug("DefaultParser.validate() result:" + result);
return result;
}
public static void main(String[] args) throws Exception{
BufferedInputStream input =
new BufferedInputStream(
new FileInputStream("po.xml")); BufferedInputStream schema =
new BufferedInputStream(
new FileInputStream("po.xsd")); XMLResource.validate(input, schema);
}
}