Thursday, July 23, 2015

Validating an XML with DTD in Java

      In this example i will show you how to validate an incoming xml . To validate an XML you have two options . One is to use XSD and second one is to use DTD's . I will explain you about XSD and why its is better compared to DTD's in a different post . In this post i will try to explain you about DTD's  and how will we validate a XML document.

 DTD's are non-xml documents which defines characteristics of an incoming xml . DTD's can be a separate document or can be included in the same XML . Lets look at a sample DTD defined XML

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE students [
   <!ELEMENT students (student+)><!-- any Number of students -->
   <!ELEMENT student (studentid, name)><!-- student can have only two elements -->
   <!ATTLIST student department CDATA #REQUIRED> <!-- department is required -->
   <!ELEMENT studentid (#PCDATA)><!-- studentId is parsable String -->
   <!ELEMENT name (#PCDATA)>
  ]>
  <students>
  <student department="computers">
     <studentid>1234</studentid>
     <name>Bran</name>
  </student>
  <student> <!--  Gives a parser Exception as department is required. -->
     <studentid>1234</studentid>
     <name>Bran</name>
  </student>
  </students>

   The DOCTYPE element is the DTD , It defines how the XML is expected to be . Lets look closely at what  this DTD means

1. The root element is students.
2. Students tag can contain one or many elements of student type. (+ indicates one or more , * indicates zero or more)
3. Student type contains two elements studentid and name.
4. Department is Required.
5. StudentId and Name should be a parsable Data .  PCDATA means parsed character data

More details on writing DTD's is found here .

Lets look how we write a java code to validate the XML . While doing a domBuilder parse , we have to set the factory to be validating true. In case you have to handle the validation errors with custom logic then you have to use the setErrorHandler method (of builder)  and pass a custom ErrorHandler Class




public class DTDValidator {

 public static void main(String[] args) throws SAXException,ParserConfigurationException,IOException {
 
  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  domFactory.setValidating(true);
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("src\\com\\learning\\xml\\student.xml");
        //builder.setErrorHandler(<Custom Implementation Of Error Handler );
  System.out.println("Successfully Parsed");
      }
}





No comments:

Post a Comment