XML Schema Validation with JAXP

彭烨烁
2023-12-01

http://www.herongyang.com/XML-Schema/JAXP-XSD-XML-Schema-Validation-Steps.html

If you want to use the JAXP included in JDK 1.6 to validate XML documents against XML schema files, you need to follow the standard steps described below:

1. Create a SchemaFactory instance by selecting the default implementation of the XML Schema language. For example, sfactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI). W3C_XML_SCHEMA_NS_URI is pre-defined constant of "http://www.w3.org/2001/XMLSchema", the fully-qualified URI for W3C XML Schema language.

2. The SchemaFactory instance can then be used to read a schema file, parse it, and create a Schema instance. for example, schema = sfactory.newSchema(new File(name)). The newSchema() method will throw exceptions, if there are any errors while parsing the specified schema file.

3. When the Schema instance is loaded with the XSD file, it can be used to create a Validator instance. The Validator instance is now ready to be used to validate any XML files presented as a DOMSource, SAXSource or other XML sources. For example, validator = schema.newValidator().

4a. One way to feed the XML file to the Validator instance is to present it as a DOMSource object. This can be done in 4 steps:

  • Create a DocumentBuilderFactory instance with the default implementation class. For example, bfactory = DocumentBuilderFactory.newInstance().
  • The DocumentBuilderFactory instance can then be used to create a DocumentBuilder instance with the default implementation class. For example, builder = bfactory.newDocumentBuilder().
  • The DocumentBuilder instance can then be used to read the XML file, parse it, and create a Document instance. For example, document = builder.parse(new File(name)).
  • The last step is to simply convert the Document instance into a DOMSource object using the constructor. For example, source = new DOMSource(document).

4b. Another way to feed the XML file to the Validator instance is to present it as a SAXSource object. This can be done in 3 steps:

  • First open the XML file as a FileInputStream instance. For example, stream = new java.io.FileInputStream(name).
  • Then convert the FileInputStream instance as an InputSource instance. For example, input = new InputSource(stream).
  • The last step is to simply convert the InputSource instance into a SAXSource object using the constructor. For example, source = new SAXSource(input).

5. With the XSD file parsed and represented as a Validator instance, and the XML file represented as a Source object, validating the XML file can be done by calling the validate() method. For example, validator.validate(source).

Several tutorial examples will be provided in this chapter to show you how to perform different steps mentioned above.

 

 

 

================================

http://weblogs.java.net/blog/spericas/archive/2007/03/xml_schema_vali.html

XML Schema Validation with JAXP 1.4 and JDK 6.0

Posted by spericas on March 1, 2007 at 11:20 AM PST

A few people have found problems validating DOM instances with JAXP1.4/JDK 6.0. I saw this quesion raised in the Java Technology and XML forum, and at least 3 bugs were filed for this in the last few weeks. I'll use this blog entry to explain what the problem is and how to easily fix your code.

Let's start by showing a snippet of the problematic code, which basically parses an XML file into a DOM and tries to validate it against an XML schema.


     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder parser = dbf.newDocumentBuilder();
     Document document = parser.parse(getClass().getResourceAsStream("test.xml"));
            
     // create a SchemaFactory capable of understanding XML schemas
     SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            
     // load an XML schema
     Source schemaFile = new StreamSource(getClass().getResourceAsStream("test.xsd"));
     Schema schema = factory.newSchema(schemaFile);
            
     // create a Validator instance and validate document
     Validator validator = schema.newValidator();
     validator.validate(new DOMSource(document));

Can you spot any problems in this code? Well, there's nothing obviously wrong with it, except that if you try this with JAXP 1.4 RI or JDK 6.0, you're going to get an error like,


     org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration 
           of element 'foo'. at ...

So what is the problem then? Namespace processing. By default, and for historical reasons, namespace awareness isn't enabled in a DocumentBuilderFactory, which means the document that is passed to the validator object isn't properly constructed. You can fix this problem by adding just one line,


     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     dbf.setNamespaceAware(true);      // Must enable namespace processing!!
     DocumentBuilder parser = dbf.newDocumentBuilder();
     ...

By now you may be asking yourself why is this even reported as a problem. Naturally, XML Schema validation requires namespace processing. It turns out that in JDK 5.0 this magically worked in many cases. Notice the use of the term "magically". By that I mean, "we don't know how it worked before but we are certainly looking into it".

So for now I'm just reporting the problem and proposing a fix for it. But I still owe you a better explanation as to why this worked before --or maybe you know why and you can tell me?

 

 类似资料:

相关阅读

相关文章

相关问答