cvc-elt.1: Cannot find the declaration of element---与spring 无关的schema 验证失败
晚上查了好久,都是spring 出这种问题的解决方式,终于查到为什么了。
http://wakan.blog.51cto.com/59583/7218/ 转自这个人。。 多谢啦! 为了验证 XML 文档的合法性,通常需要对文档进行验证,要么符合 DTD,要么符合 Schema。在以前的 JAXP 版本中,验证(Validation)是作为解析器(Parser)的一个功能特征来提供的。 JAXP 的 Validation API 把 XML Parser 和 Validator 解耦合了。这样获得了两个好处: 1、支持更多的 Schema 语言 2、在运行时 XML 与 Schema 更容易耦合在一起 以下是一个小例程,演示了用 Validation API 验证 XML 文件是否符合 Schema: import java.io.*;
import javax.xml.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*; public class TestMain { public static void main(String[] args) {
try {
String strLang = XMLConstants.W3C_XML_SCHEMA_NS_URI;
SchemaFactory factory = SchemaFactory.newInstance(strLang); InputStream isSchema = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal.xsd");
StreamSource ss = new StreamSource(isSchema);
Schema schema = factory.newSchema(ss); Validator validator = schema.newValidator(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml");
Document document = db.parse(isXML); DOMSource source = new DOMSource(document);
validator.validate(source);
System.out.println("Result : Valid!"); } catch (Exception e) {
e.printStackTrace();
System.out.println("Result : Invalid!");
}
}
} 运行环境:JDK 1.4.2 + Xerces 2.8.0 for Java。 有一个小地方要注意,如果用的是 DOMSource,程序中必须加上:dbf.setNamespaceAware(true);
否则会出错,找不到根节点元素的定义,例如: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'personnel'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
at TestMain.main(TestMain.java:32) 当然,如果用的是 StreamSource,就不需要这么费劲了。如下: ……
InputStream isXML = ClassLoader.getSystemClassLoader()
.getResourceAsStream("personal-schema.xml"); /*
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(isXML);
DOMSource source = new DOMSource(document);
*/ StreamSource source = new StreamSource(isXML);
validator.validate(source);
…… 附源代码下载。是一个 Eclipse 工程。为了减少字节数,把 lib 目录下 Xerces 2.8.0 for Java 的 Jar 包删除掉了。下载后,请自行加上:xml-apis.jar 和 xercesImpl.jar。
cvc-elt.1: Cannot find the declaration of element---与spring 无关的schema 验证失败的更多相关文章
- cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.0.xsd'
Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'beans' ...
- spring cvc-elt.1: Cannot find the declaration of element 'beans'解决办法
转载自http://blog.csdn.net/legendj/article/details/9950963 今天在写spring aop示例的时候,在spring.xml文件中添加spring a ...
- 解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element
解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element 'beans'.Referenced file conta ...
- Spring 异常 —— cvc-elt.1: Cannot find the declaration of element 'beans'
有个使用 Spring 的项目,运行时报错: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 ...
- cvc-elt.1: Cannot find the declaration of element 'beans'
@(编程) 现象描述 导入的一个eclipse项目报错,各种方法都无法解决,报错信息如下: cvc-elt.1: Cannot find the declaration of element 'bea ...
- Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'(转)
Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans' 2008-09-07 22:41 今天把在线聊天室代码改了下 ...
- Cannot find the declaration of element 'ehcache'.
ehchahe.xml中报错: Cannot find the declaration of element 'ehcache'. 由于 <?xml version="1.0" ...
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
1缺少jar包 2spring配置文件里http://www.springframework.org/schema/beans/spring-beans-3.2.xsd的版本与实际jar包不一致
随机推荐
- 【Qt】Qt环境搭建(Qt Creator)【转】
简述 上一节中介绍了如何进行Qt和Visual Studio的下载安装,随后演示了如何将Qt集成到Visual Studio中,并完成了我们第一个Qt小程序-Hello World.下面主要讲解如何利 ...
- linux命令详解之chkconfig命令使用方法
介绍一个linux常用命令,chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:c ...
- Color About——Second
下面来简要的说一下Android开发中如何对某一个Activity进行背景色的设置.下面我以名字为FirstActivity的Activity的背景色的设置进行说明,先说一下Drawable类: 关于 ...
- Delphi XE5教程1:语言概述
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...
- 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)
实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...
- Hive基本命令整理
创建表: hive> CREATE TABLE pokes (foo INT, bar STRING); Creates a table called pokes with t ...
- Oracle之存储过程
1.存储过程创建 oracle中创建存储过程的语法如下: CREATE [OR REPLACE] PROCEDURE PRO_NAME[(parameter1[,parameter2]...)]is| ...
- 多线程中,static函数与非static函数的区别?
最近在学习多线程,刚入门,好多东西不懂,下面这段代码今天想了半天也没明白,希望看到的兄弟姐妹能解释下. public class NotThreadSafeCounter extends Thread ...
- TTL值的含义以及与域名DNS TTL值的区别
TTL值的含义以及与域名TTL值的区别 本文来源于时光漂流瓶 http://www.9usb.net , 原文地址: http://www.9usb.net/201004/ttl-yuyuming-t ...
- 为什么我们使用192.168.0.1作为内网ip
私有IP地址是一段保留的IP地址.只是使用在局域网中,在Internet上是不使用的. 私有IP地址的范围有: 10.0.0.0-10.255.255.255 172.16.0.0—172.31.25 ...