Why datatype isn't verified when validating XML with Schema?
Here is XML Schema:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="id" type="xs:long" /> </xs:schema>
XML file that is validated for conformance:
<?xml version="1.0" ?> <id>invalid_data</id>
Java code that works with XML and should test conformance:
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); factory.setSchema(schemaFactory.newSchema(new File(xmlSchema))); parser = factory.newSAXParser(); MySaxHandler handler = new MySaxHandler(); parser.parse(new File(xmlFile), handler);
During invocation of this code NumberFormatException is throwed in characters method of MySaxHandler. MySaxHandler also contains overriden methods warning, error and fatalError but they aren't executed.
I don't want this NumberFormatException being thrown. I want datatype being validated according to Schema. How do I do it?
I wonder if it's simply a timing issue. The schema validator can't test the content of the element until the end element event is reached, but the text node is reported to the ContentHandler before the end element event is reached. Try masking the exception in your application to see if the validation error gets reported a bit later.