7

I want to parse a xml file using a SAXParser or XMLReader and verify that the file conforms to a specific xsd file (new File( "example.xsd" )).

It's easy to

  1. do the validation against a xsd file in an extra step using a Validator like in this SO answer.

  2. to validate while parsing by specifying the name of the xsd as "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" like in this SO answer.

But how can I validate against a new File( "example.xsd" ) while parsing?

Community
  • 1
  • 1
tangens
  • 39,095
  • 19
  • 120
  • 139

1 Answers1

5

Assuming Java 5 or above, set the schema on the SAXParserFactory:

SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("myschema.xsd"));
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setSchema(schema);
SAXParser parser = saxFactory.newSAXParser();
parser.parse("data.xml", new DefaultHandler() {
  // TODO: other handler methods
  @Override
  public void error(SAXParseException e) throws SAXException {
    throw e;
  }
});

You handle validation errors by overriding the error method on your handler and acting as you see fit.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • It seems that the parser still uses the schema defined inside the xml file and not the one I set with `schemaFactory( newSchema(...))`. – tangens Oct 27 '09 at 13:13
  • 1
    That isn't the behaviour I see (the parser always validates against the schema set on the factory, even if one is set in the document). Without sample documents/code, it is difficult to identify the problem. – McDowell Oct 27 '09 at 13:49