Cvc-elt.1:cannot find the declaration of element---failed with spring-independent schema validation

Source: Internet
Author: User
Tags xml parser

The night looked for a long time, are spring out of this problem solution, finally found out why.

Http://wakan.blog.51cto.com/59583/7218/turned from this man. Thank you! In order to validate the validity of an XML document, it is often necessary to validate the document, either in a DTD or in accordance with the Schema.  In previous versions of JAXP, validation (Validation) was provided as a feature feature of the parser (Parser). The JAXP Validation API has decoupled the XML Parser and Validator. This gives you two benefits: 1, support for more schema language 2, the XML is more easily coupled to the schema at run time the following is a small routine that demonstrates verifying that an XML file conforms to schema:i with the Validation API  Mport 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!");  }}} Run Environment: JDK 1.4.2 + Xerces 2.8.0 for Java.     There is a small place to note, if the use of Domsource, the program must be added: Dbf.setnamespaceaware (true);    Otherwise, the definition of the root node element could not be found, for example: Org.xml.sax.saxparseexception:cvc-elt.1:cannot find the declaration of the 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.XMLSch Emavalidator.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.xerc Es.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) Of course, if you use Streamsource, You don't have to be so laborious.    As follows:......        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);  ...... Attached source code download. is an Eclipse project. To reduce the number of bytes, the Xerces 2.8.0 for Java Jar package is removed from the Lib directory. After downloading, please add: Xml-apis.jar and Xercesimpl.jar.

  

Cvc-elt.1:cannot find the declaration of element---failed with spring-independent schema validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.