晚上查了好久,都是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 验证失败的更多相关文章

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

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

  3. 解决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 ...

  4. Spring 异常 —— cvc-elt.1: Cannot find the declaration of element 'beans'

    有个使用 Spring 的项目,运行时报错: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 ...

  5. cvc-elt.1: Cannot find the declaration of element 'beans'

    @(编程) 现象描述 导入的一个eclipse项目报错,各种方法都无法解决,报错信息如下: cvc-elt.1: Cannot find the declaration of element 'bea ...

  6. 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 今天把在线聊天室代码改了下 ...

  7. Cannot find the declaration of element 'ehcache'.

    ehchahe.xml中报错: Cannot find the declaration of element 'ehcache'. 由于 <?xml version="1.0" ...

  8. idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.

    ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...

  9. 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包不一致

随机推荐

  1. MongoDB的分组统计 group

    mongodb中的分组聚合用$group,而且处理的最大数据量为100M如果超出需要写入到磁盘,使用格式如下: { $group: { _id: <expression>, <fie ...

  2. maven 相关

    maven相关 一 windows安装配置maven: 1. 官网下载最新版本maven,发布日志时为: maven3.2.1 2.解压maven到相应的目录:配置环境变量: MAVEN_HOME:D ...

  3. SOAP 介绍

    简介 SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议.此协议规范由 IBM.Microso ...

  4. C# 缓存学习第一天

    缓存应用目的:缓存主要是为了提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容量数据时,使用缓存来直接为客户端服务,可以减少客户端与服务器端的数据交互,从而大大提高程序的性能 ...

  5. .Net码农学Android---前言

    自从毕业参加工作后,就一直想学移动领域得开发,但时间.精力.决心.学习成本等这些问题总在不同程度的阻碍着自己. 但这段时间自己想做一款属于自己的App的想法越来越强烈,我感到自己快压不住这股能量了.终 ...

  6. MVC学习系列——Model验证扩展

    MVC中,实现了前端后端的验证. 前端验证.步骤: web.config这两个得开启: <add key="ClientValidationEnabled" value=&q ...

  7. iOS常见问题(2)

    一.模拟器黑屏 解决方法: 二.打代码时,Xcode没提示 解决方法: 0. 点击Preferences 1. 进入Text Editing 2. 勾选 三.有时候可能在勾选 Autolayout的时 ...

  8. 用 HTML 编写博客栏目

    “生活中不是缺少美,而是缺少发现美的眼睛. -----罗丹 在之前阅读师哥师姐们博客的时候,尤其是那些感觉非常优秀博客的时候就注意到了一个东西..... ----------------------- ...

  9. cocos2dx中的内存管理机制及引用计数

    1.内存管理的两大策略: 谁申请,谁释放原则(类似于,谁污染了内存,最后由谁来清理内存)--------->适用于过程性函数 引用计数原则(创建时,引用数为1,每引用一次,计数加1,调用结束时, ...

  10. 特征值分解,奇异值分解(SVD)

    特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. 1. 特征值: 如果说一个向 ...