晚上查了好久,都是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. 使用ANT 生成Xfire 客户端端文件

    这里需要用到的JAR包 : XmlSchema-1.1.jar activation-1.1.jar commons-codec-1.3.jar commons-httpclient-3.0.jar ...

  2. struct和class区别

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  3. LINQ技巧:如何通过多次调用GroupBy实现分组嵌套

    问题如上,解决如下,目标在最下面:结果: using System; using System.Linq; using System.Collections.Generic; namespace Co ...

  4. 单元测试SimpleTest新手入门

    最近学习单元测试,先用了下PHPunit,结果安装问题一大堆,于是立刻放弃改试simpletest,感觉简单多了.下面列出步骤. 1.下载simpletest(版本1.1.0), http://www ...

  5. jeecms子栏目或者文章页导航父栏目选中解决方法

    jeecms在子栏目或者文章页导航父栏目选中,看例子 <div class="nav"> <ul> [@cms_channel_list ] <li ...

  6. Android 中的WiFi剖析

    Android的WiFi 我们通常看到WiFi的守护进程wpa_supplicant在我们的ps的进程列表中,这个就是我们的wifi守护进程.wpa_supplicant在external/wpa_s ...

  7. Redis 二:入门基本篇

    .多数据库设置 - ,代表16个数据库 .glob风格通配符 keys * 返回所有 keys ba? 返回 bar keys b[a-z][a-z] 返回btt bar \x 匹配转义字符 .判断一 ...

  8. 银行HR:寒门再难出贵子

    银行HR:寒门再难出贵子来源:金融行业网 2013 年 8 月 6 日 来源:天涯社区 作者:永乐大帝二世 本文是一位银行的HR写的,他工作了10年,接待了一群到银行实习的实习生,然后观察他们发生的好 ...

  9. Fragment inner class should be static

    package com.example.fragmenttest; import android.annotation.SuppressLint; import android.app.Activit ...

  10. SpringMVC+redis整合

    在网络上有一个很多人转载的springmvc+redis整合的案例,不过一直不完整,也是被各种人装来转去,现在基本将该框架搭建起来. package com.pudp.bae.base; import ...