• 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。
    

      

    I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
  • 相关阅读:
    Ubuntu杂记——Ubuntu自带拼音输入发杂乱不堪
    数据库随笔——数据库设计原则(转)
    Java学习笔记——回调函数
    Android随笔之——静默安装、卸载
    Hibernate学习之——搭建log4j日志环境
    Hibernate学习之——Hibernate环境搭建
    Android随笔之——闹钟制作铺垫之AlarmManager详解
    Android随笔之——Android时间、日期相关类和方法
    Nyoj 天下第一(spfa)
    hdu 最大三角形(凸包+旋转卡壳)
  • 原文地址:https://www.cnblogs.com/IamThat/p/4346014.html
Copyright © 2020-2023  润新知