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

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">
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

Here is the DTD File: Employee.dtd

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

Here is the Java file: DOMValidateDTD.java

import java.io.*;
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;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class DOMValidateDTD {
  public static void main(String args[]) {  
  try{
  DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance
();
  factory.setValidating(true);
  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)
  throws 
SAXParseException {
  System.out.println("Error at " +e.getLineNumber() " line.");
  System.out.println(e.getMessage());
  System.exit(0);
  }
  //Show warnings
  public void warning(SAXParseException err)
  throws 
SAXParseException{
  System.out.println(err.getMessage());
  System.exit(0);
  }
  });
  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.setOutputProperty(
  OutputKeys.DOCTYPE_SYSTEM, 
"Employee.dtd");
  transformer.transform(source, result);
  }
  catch (Exception e) {
  System.out.println(e.getMessage());
  }
  }
}

Download this example.

Output of this program:

C:\vinod\xml>javac DOMValidateDTD.java

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