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; public class TestXMLValidation { @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 ...