Results 1 to 2 of 2
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default DocumentBuilder.parse() prints error but doesn't throw exception

    I've set a schema on my DocumentBuilderFactory. The DocumentBuilders it produces print an error when they parse documents that don't comply with the schema, but they don't throw an exception like I expected.

    My code:

    Java Code:
    	private static void parseXML() throws SAXException, IOException, ParserConfigurationException {
    
    		System.out.print("Loading " + SCHEMA + " . . . ");
    		StreamSource schemaSource =
    			new StreamSource(Generator.class.getResourceAsStream(SCHEMA));
    		SchemaFactory sf =
    			SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    		Schema schema = sf.newSchema(schemaSource);
    		System.out.println("OK");
    		
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		dbf.setNamespaceAware(true);
    		dbf.setSchema(schema);
    		DocumentBuilder builder = dbf.newDocumentBuilder();
    		System.out.print("Parsing " + REQUESTS + " . . . ");
    		requestsDoc = builder.parse(Generator.class.getResourceAsStream(REQUESTS));
    		System.out.println("OK");
    		System.out.print("Parsing " + DATA + " . . . ");
    		dataDoc = builder.parse(Generator.class.getResourceAsStream(DATA));
    		System.out.println("OK");
    		
    		protocolDocs = new Document[]{requestsDoc, dataDoc};
    		
    	}
    The output:

    Java Code:
    Loading /resource/Protocol.xsd . . . OK
    Parsing /resource/Requests.xml . . . OK
    Parsing /resource/Data.xml . . . [Error] :16:31: cvc-enumeration-valid: Value 'PRE_LOGIN' is not facet-valid with respect to enumeration '[HANDSHAKING, AUTHENTICATION, GAME_SELECTION, GAME]'. It must be a value from the enumeration.
    [Error] :16:31: cvc-type.3.1.3: The value 'PRE_LOGIN' of element 'Context' is not valid.
    OK
    As you can see, the validator correctly identified a problem, but execution continued. Can I make it throw an exception? Do I need to set something with DocumentBuilderFactory.setFeature?

    Edit: I found a workaround - http://stackoverflow.com/questions/2...-5-and-jdk-1-6 Anyone know if this is actually a bug in 1.6, and the workaround is the best solution, or if there's a prescribed way to make DocumentBuilder.parse throw exceptions?
    Last edited by kjkrum; 05-31-2011 at 10:53 PM.
    Get in the habit of using standard Java naming conventions!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Nevermind, I figured it out. (There needs to be the name for the magical effect of figuring things out right after you post about them.)

    I added this error handler to my DocumentBuilder:

    Java Code:
    		// make it choke hard on every little thing
    		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;					
    			}
    		});
    Apparently, not complying with the schema only merits a warning according to the default error handler.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Throw Exception in this code
    By eng_hyzoom in forum New To Java
    Replies: 9
    Last Post: 12-22-2010, 06:18 PM
  2. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM
  3. Replies: 0
    Last Post: 12-04-2009, 12:39 PM
  4. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 04:35 AM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •