All IT Courses 50% Off
JAVA Tutorials

How to Validate XML using XSD/DTD?

An XML document that is well created can be validated using DTD (Document Type Definition) or XSD (XML Schema Definition). A well-formed XML document should have correct syntax and should follow the below rules:

  • It must start with the XML declaration.
  • It must have one unique root element enclosing all the other tags.
  • All start tags must have end tags.
  • XML tags are case-sensitive.
  • All elements must be closed with the proper ending tag.
  • All elements must be nested properly.
  • All attributes’ values must be in quotes.
  • For special characters, XML entities must be used.


DTD defines the structure of the document containing a list of all the legal elements and attributes. The main motive of the DTD is to define the structure of an XML document.

We must avoid errors in the XML documents because any error will stop the execution of XML programs


Step 1. Create an XML file:

<?xml version="1.0"?>
  <!DOCTYPE list SYSTEM "simple_recipe.dtd">
    <recipe_name>Chocolate Chip Bars</recipe_name>
      <item>2/3 Cup butter</item>      <item>2 Cup brown sugar</item>
      <item>1 tsp vanilla</item>     <item>1 3/4 Cup all-purpose flour</item>
      <item>1 tsp baking powder</item>
      <item>pinch of salt</item>      <item>2 eggs</item>
      <item>1/2 Cup chopped nuts</item>
      <item>2 cups (12-oz pkg.) semi-sweet choc. chips</item>
 Preheat oven to 350 degrees. 
 Melt the butter; add brown sugar and vanilla in a large mixing bowl. 
 Set aside to cool it down. Mix the all-purpose flour, baking powder, and salt and keep it aside. 
 Add eggs to the cooled sugar mixture and beat well. 
 Stir and add dry ingredients, nuts, and chips. 
 Grease a 13-by-9-inch pan with butter.
 Bake for 30 to 40 minutes until it turns golden brown and then waits for it cool down. After it is completely cool, cut into square pieces and serve.

Step 2. Create a DTD file:

<!ELEMENT list (recipe+)>  
<!ELEMENT recipe (author, recipe_name, meal, ingredients, directions)> 
<!ELEMENT author (#PCDATA)>  
<!ELEMENT recipe_name (#PCDATA)>  
<!ELEMENT meal (#PCDATA)>  
<!ELEMENT ingredients (item+)>  
<!ELEMENT item (#PCDATA)>  
<!ELEMENT directions (#PCDATA)> 


XSD is used as an alternative to DTD. XSD is also used to create a well formed XML document. To reuse the existing definitions namespaces are used in XSD.


Step 1. Create an XML file:

<?xml version="1.0" encoding="UTF-8"?>
            <title>Brand New Eyes</title>
            <genre>Punk Rock</genre>
            <artist>Various Artist</artist>

Step 2. Create an XSD file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="" attributeFormDefault="unqualified"
    <xs:element name="root" type="rootType">

    <xs:complexType name="rootType">
            <xs:element name="records" type="recordsType"/>

    <xs:complexType name="recordsType">
            <xs:element name="record" type="recordType" maxOccurs="unbounded" minOccurs="0"/>

    <xs:complexType name="recordType">
            <xs:element type="xs:string" name="title"/>
            <xs:element type="xs:string" name="artist"/>
            <xs:element type="xs:string" name="genre"/>
            <xs:element type="xs:short" name="year"/>

Step 3. Create a JAVA code:

import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.util.Objects;

public class XMLValidator {
    public static final String XML_FILE = "records.xml";
    public static final String SCHEMA_FILE = "records.xsd";

    public static void main(String[] args) {
        XMLValidator XMLValidator = new XMLValidator();
        boolean valid = XMLValidator.validate(XML_FILE, SCHEMA_FILE);

        System.out.printf("%s validation = %b.", XML_FILE, valid);

    private boolean validate(String xmlFile, String schemaFile) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            Schema schema = schemaFactory.newSchema(new File(getResource(schemaFile)));

            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(getResource(xmlFile))));
            return true;
        } catch (SAXException | IOException e) {
            return false;

    private String getResource(String filename) throws FileNotFoundException {
        URL resource = getClass().getClassLoader().getResource(filename);

        return resource.getFile();

Difference between DTD and XSD:

1DTD refers to Document Type Definition.XSD refers to XML Schema Definition.
2These are derived from SGML syntax.These are written in XML.
3It does not support datatypes.It supports datatypes.
4It does not support namespaces.It supports namespaces.
5It does not define the order of child elements.It defines the order of child elements.
6It is not extensible.It is extensible.
7It is not simple and easy to learn.It is simple and easy to learn.
8It provides less control over the XML structure.It provides more control over the XML structure.
Facebook Comments

One Comment

  1. is it possible to validate XML Document by DTD & XSD at the same time? what do i mean is the DTD & XSD validate code is inserted on the XML File. like this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button

Get Java Training Online
Course Now at 50%OFF!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys will use this information to be in touch with you for providing updates.

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.