Using a DTD to Verify an XML Document

DocumentBuilderFactory factory =
											DocumentBuilderFactory.newInstance();
											factory.setValidating(true);
											DocumentBuilder builder =
											factory.newDocumentBuilder();

A Document Type Definition (DTD) file defines how a particular XML document type should be structured. For example, a DTD will specify which elements, attributes, and so on are permitted in a document. An XML document that conforms to a DTD is considered to be valid. An XML document that is syntactically correct, but does not conform to a DTD, is said to be well-formed.

To validate a document using a DTD, we simply need to call the setValidating() method of the DocumentBuilderFactory instance and pass a value of true. Any XML documents that we parse will ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.