Share on Google+Share on Google+

XML Validate DTD

In this section, you will learn to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. A DTD defines the document structure with a list of legal elements and attributes.

XML Validate DTD 


In this section, you will learn to validate a xml file against a  DTD (Document Type Definition)  using the DOM APIs. A DTD defines  the document structure with a list of legal elements and attributes.

Description of program:

Validating a XML file against a DTD needs a xml file and its DTD document. First of all construct a well-formed xml file along with a DTD file . This DTD file defines all elements to keep in the xml file. After creating these, we parse the xml file using the parse() method and generates a Document object tree. The setErrorHandler() method invokes an object of DoucmentBuilder. Enable the setValidating() method of the factory to "true".  If we pass 'true' the parser will validate xml documents otherwise not. To validate xml file , pass the DTD file as setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "Employee.dtd") in the transformer object.

Here is the xml file: Employeexy.xml

<?xml version = "1.0" ?>
<!DOCTYPE Employee SYSTEM "Employee.dtd">
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

Here is the DTD File: Employee.dtd

<!ELEMENT Employee (Emp_Id, Emp_Name, Emp_E-mail)>
<!ELEMENT Emp_E-mail (#PCDATA)>

Here is the Java file:

import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;

public class DOMValidateDTD {
  public static void main(String args[]) {  
  DocumentBuilderFactory factory = 
  DocumentBuilder builder = factory.newDocumentBuilder();
  builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
  //Ignore the fatal errors
  public void fatalError(SAXParseException exception)
throws SAXException { }
  //Validation errors 
  public void error(SAXParseException e)
SAXParseException {
  System.out.println("Error at " +e.getLineNumber() " line.");
  //Show warnings
  public void warning(SAXParseException err)
  Document xmlDocument = builder.parse(
new FileInputStream("Employeexy.xml"));
  DOMSource source = new DOMSource(xmlDocument);
  StreamResult result = new StreamResult(System.out);
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer transformer = tf.newTransformer();
  transformer.transform(source, result);
  catch (Exception e) {

Download this example.

Output of this program:


C:\vinod\xml>java DOMValidateDTD
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE Employee SYSTEM
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>


Posted on: June 20, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: XML Validate DTD   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Vishnu Mohite
May 7, 2012
How to Converted XML Schema to DTD using the Java.

How to Converted XML Schema to DTD using the Java. Please provide the apropreate Solution.
Ivaylo Metodiev
June 14, 2013

Thank you so much. It helped me a great deal with my project.
July 1, 2013

<SOAP-ENV:Envelope xmlns:wsa="" xmlns:SOAP-ENV="" xmlns:wsu=""><SOAP-ENV:Header><wsse:Security xmlns:wsse=""><wsse:UsernameToken><wsse:Username>GSS</wsse:Username><wsse:Password Type="">letmein</wsse:Password></wsse:UsernameToken></wsse:Security><wsa:Action>AccountFindProofDetails</wsa:Action><wsa:To></wsa:To><wsa:MessageID>NBKOLPD52411350</wsa:MessageID></SOAP-ENV:Header><SOAP-ENV:Body><AccountFindProofDetailsRequest xmlns=""><InputHeader xmlns=""><OpenGBSMessageVersion><MessageType>OGIM</MessageType><MessageMajorVersion>01</MessageMajorVersion><MessageMinorVersion>00</MessageMinorVersion></OpenGBSMessageVersion><ClientData><ClientApplicationID>GSS</ClientApplicationID><ClientMessageID>NBKOLPD0524113500</ClientMessageID><ClientTimeStamp>2013-05-24T11:35:00Z</ClientTimeStamp></ClientData><LEOData><LEOFunction>GSSINQ</LEOFunction><LEOUserID>6046GSSIN</LEOUserID><LEOPassword>********</LEOPassword><LEOBranch>7114</LEOBranch></LEOData><ClassMethod><RequestedClass>GBSAccount</RequestedClass><RequestedMethod>FindProofDetails</RequestedMethod><MajorVersion>0000</MajorVersion><MinorVersion>0001</MinorVersion></ClassMethod></InputHeader><InputApplicationData><BranchNumber>7114</BranchNumber><AccountNumber>30303994</AccountNumber><OutstandingsType>2</OutstandingsType><FromAmount/><ToAmount/><NextSequenceNumber/><MaxCount>0045</MaxCount></InputApplicationData></AccountFindProofDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>