晚上查了好久,都是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. 例题6-8 Tree Uva548

    这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...

  2. [Android教程]TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  3. js根据IP取得天气

    <span id="weather"></span> <script> function weather(cityName) { var cha ...

  4. 有趣的checkbox动画切换状态(支付宝转账成功显示)--第三方开源--AnimCheckBox

    这个很有趣的指标通过AnimCheckBox实现,下载地址:https://github.com/lguipeng/AnimCheckBox 代码: activity_main.xml: <Re ...

  5. Translation001——android

    请尊重原创,转载请注明出处: Author:KillerLegend Link:http://www.cnblogs.com/killerlegend/ BEGIN****************** ...

  6. RouterOS的MikroTik脚本从DNS更新IPSEC端的IP地址

    #Script for changing IPSEC address when DNS changes. #Script will iterate through all peers looking ...

  7. Excel中的宏--VBA的简单例子

    第一步:点击录制宏 第二步:填写宏的方法名 第三步:进行一系列的操作之后,关闭宏 第四步:根据自己的需要查看,修改宏 第六步:保存,一般是另存为,后缀名为.xlsm,否则宏语言不能保存. 到此为止恭喜 ...

  8. postgresql 开启远程访问

    1.如果服务器启用了防火墙,需要在防火墙上开启 5432 端口. 2.修改 PostgreSQL 配置文件 postgresql.conf.postgresql.conf,Linux 配置文件所在路径 ...

  9. C#——中文转化成拼音

    在KS系统中用到了中文转化成拼音的功能.通过查阅资料为下面是代码. /// <summary> /// MyConvert 的摘要说明 /// </summary> publi ...

  10. Android系统SVC命令教程

    svc命令,位置在/system/bin目录下,用来管理电源控制,无线数据,WIFI # svc svc Available commands: help Show information about ...