Validating XML against DTD

Associate
Joined
23 Feb 2004
Posts
508
Location
London
I have a number (over 250) of XML files which I need to check against a DTD file to make sure they comply.

What's the best way of doing this programmatically, preferably in Java?
 
Code:
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("pathToXML.xml"));
	
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	
Source schemaFile = new StreamSource(new File("pathToDTD.dtd"));
Schema schema = factory.newSchema(schemaFile);
	
Validator validator = schema.newValidator();

validator.validate(new DOMSource(document));

I'm trying to use the code above but i keep on getting this error from factory.newSchema(schemaFile); "The markup in the document preceding the root element must be well-formed."

I think it's because it's a DTD file and not an XSD file.

The only thing I can see to change is the W3C_XML_SCHEMA_NS_URI but apparently the only other option I have is RELAXNG_NS_URI, which isn't right.

Any help would be nice. Thanks.
 
Back
Top Bottom