Preview:
//internal_dtd.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customers [
    <!ELEMENT customers (customer+)>
    <!ELEMENT customer (name, email, phone)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT email (#PCDATA)>
    <!ELEMENT phone (#PCDATA)>
]>
<customers>
    <customer>
        <name>Satyam Nayak</name>
        <email>Satyam@nayak.com</email>
        <phone>122-112-1234</phone>
    </customer>
    <customer>
        <name>Sonu N</name>
        <email>Sonu@N.com</email>
        <phone>112-554-9969</phone>
    </customer>
</customers>
// ValidateInternalDTD.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

public class ValidateInternalDTD {
    public static void main(String[] args) {
        try {
            File xmlFile = new File("internal_dtd_example.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setValidating(true); // Enable validation
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            dBuilder.setErrorHandler(new SimpleErrorHandler());
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            System.out.println("XML is valid with Internal DTD.");
        } catch (ParserConfigurationException | SAXException | IOException e) {
            System.out.println("XML is invalid: " + e.getMessage());
        }
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter