P
Paco
I'm trying to validate some XML against a schema. The code I have
works but it won't give me any useful information for where the error
is in the XML. I can get useful information (line numbers) if there
are problems when loading the schema and XML. However, the exceptions
generated from validate() always have line and column numbers of "-1".
Am I doing something wrong or should I be going about this in a totally
different method? My code is below (minus error handling and such).
The SAXErrorHandler is a class I created that simply prints out the
message and line and column numbers.
// Load Schema
File schemaFile = new File("schema.xsd");
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(schemaFile);
Schema schema = factory.newSchema(schemaSource);
// Load XML
File xmlFile = new File("data.xml");
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = parser.parse(xmlFile);
// Validate
Validator validator = schema.newValidator();
SAXErrorHandler errHandler = new SAXErrorHandler();
validator.setErrorHandler(errHandler);
DOMResult result = new DOMResult();
validator.validate(new DOMSource(document), result);
works but it won't give me any useful information for where the error
is in the XML. I can get useful information (line numbers) if there
are problems when loading the schema and XML. However, the exceptions
generated from validate() always have line and column numbers of "-1".
Am I doing something wrong or should I be going about this in a totally
different method? My code is below (minus error handling and such).
The SAXErrorHandler is a class I created that simply prints out the
message and line and column numbers.
// Load Schema
File schemaFile = new File("schema.xsd");
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(schemaFile);
Schema schema = factory.newSchema(schemaSource);
// Load XML
File xmlFile = new File("data.xml");
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = parser.parse(xmlFile);
// Validate
Validator validator = schema.newValidator();
SAXErrorHandler errHandler = new SAXErrorHandler();
validator.setErrorHandler(errHandler);
DOMResult result = new DOMResult();
validator.validate(new DOMSource(document), result);