0

I need to validate a multiple files produced by a third party against a DTD; The files don't have dtd declaration and I can't change this because I am not the owner, Is there a way of validating them using dom4j by providing the DTD separately?

Thanks

John Stadt
  • 500
  • 7
  • 17
  • If push comes to shove, you can always run the input through a pipeline that inserts the string " " at the top of the input document, after any existing XML declaration. (Better, of course, if there's a less hacky way, but there I can't help you.) – C. M. Sperberg-McQueen Sep 26 '12 at 20:11
  • yes, that's the problem; I have to validate about 250 files at once so looking for a better option :-) – John Stadt Sep 27 '12 at 16:37

1 Answers1

0

You can always convert the DTD to a schema (using for example trang), and perform an in-memory validation against the schema for each document.

To do that, you would create one javax.xml.validation.ValidationHandler with the converted schema, and feed it to a DOM4J SAXWriter.

Another option is of course to

  1. Parse each document without validation into a DOM4J Document
  2. Add a DOM4j DefaultDocumentType to the parsed document
  3. Write the document to a temporary string buffer
  4. Parse again, but this time with a validating SAXReader
forty-two
  • 12,204
  • 2
  • 26
  • 36