Difference in DocumentBuilder.parse when using JRE 1.5 and JDK 1.6

Recently, we have switched our projects to Java 1.6. When I ran the tests, I found that using 1.6 throws a SAXParseException that was thrown using 1.5.

Below is my test code to demonstrate the problem.

import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; import org.junit.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXParseException; /** * Test class to demonstrate the difference between JDK 1.5 to JDK 1.6. * * Seen on Linux: * * <pre> * #java version "1.6.0_18" * Java(TM) SE Runtime Environment (build 1.6.0_18-b07) * Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode) * </pre> * * Seen on OSX: * * <pre> * java version "1.6.0_17" * Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025) * Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode) * </pre> * * @author dhiller (creator) * @author $Author$ (last editor) * @version $Revision$ * @since 12.03.2010 11:32:31 */ public class TestXMLValidation { /** * Tests the schema validation of an XML against a simple schema. * * @throws Exception * Falls ein Fehler auftritt * @throws junit.framework.AssertionFailedError * Falls eine Unit-Test-Pruefung fehlschlaegt */ @Test(expected = SAXParseException.class) public void testValidate() throws Exception { final StreamSource schema = new StreamSource( new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "elementFormDefault=\"qualified\" xmlns:xsd=\"undefined\">" + "<xs:element name=\"Test\"/>" + "</xs:schema>" ) ); final String xml = "<Test42/>"; final DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance(); newFactory.setSchema( SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" ).newSchema( schema ) ); final DocumentBuilder documentBuilder = newFactory.newDocumentBuilder(); documentBuilder.parse( new InputSource( new StringReader( xml ) ) ); } } 

When using JVM 1.5, the test passes, on 1.6 it fails with "Expected exception SAXParseException".

Javadoc DocumentBuilderFactory.setSchema (Schema) Method says:

When errors are detected by the validator, the parser responsible for reporting this to the ErrorHandler user (or if the error handler is not installed, ignores them or throws them), like any other errors found by the parser itself. In other words, if the user-defined ErrorHandler is installed, it should receive these errors, and if not, they should be processed in accordance with the specific error of the default processing rule.

In the Javadoc DocumentBuilder.parse (InputSource) it says:

BTW: I tried to set up an error handler using setErrorHandler , but is still no exception.

Now my question is:

What has changed to 1.6, which prevents schema checking for SAXParseException exception? Is this related to the schema or XML I was trying to parse?

Update:

The following code works with 1.5 and 1.6, as I wanted:

  @Test(expected = SAXParseException.class) public void testValidate() throws Exception { final StreamSource schema = new StreamSource( new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "elementFormDefault=\"qualified\" xmlns:xsd=\"undefined\">" + "<xs:element name=\"Test\"/>" + "</xs:schema>" ) ); final String xml = "<Test42/>"; final DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance(); final Schema newSchema = SchemaFactory.newInstance( "http://www.w3.org/2001/XMLSchema" ).newSchema( schema ); newFactory.setSchema( newSchema ); final Validator newValidator = newSchema.newValidator(); final Source is = new StreamSource( new StringReader( xml ) ); try { newValidator.validate( ( Source ) is ); } catch ( Exception e ) { e.printStackTrace(); throw e; } final DocumentBuilder documentBuilder = newFactory.newDocumentBuilder(); documentBuilder.parse( new InputSource( new StringReader( xml ) ) ); } 

The solution is similar to explicitly using the Validator instance created from the Schema instance. I found a solution here

However, I'm not sure why this ...

+6
java xml parsing sax
source share
1 answer

Apparently, a document that does not conform to this scheme deserves only a soft reproach to stderr from the default error handler. My solution was to replace the default error handler with a more strict one:

 // builder is my DocumentBuilder builder.setErrorHandler(new ErrorHandler() { @Override public void error(SAXParseException arg0) throws SAXException { throw arg0; } @Override public void fatalError(SAXParseException arg0) throws SAXException { throw arg0; } @Override public void warning(SAXParseException arg0) throws SAXException { throw arg0; } }); 
+1
source share

All Articles