Home Xml Dom XML Validate DTD
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

XML Validate DTD

Advertisement
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">
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </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> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: June 20, 2007

Related Tutorials

Ask Questions?    Discuss: XML Validate DTD   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
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
Thanks

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

<SOAP-ENV:Envelope xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><SOAP-ENV:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>GSS</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">letmein</wsse:Password></wsse:UsernameToken></wsse:Security><wsa:Action>AccountFindProofDetails</wsa:Action><wsa:To>http://rp.baml.com</wsa:To><wsa:MessageID>NBKOLPD52411350</wsa:MessageID></SOAP-ENV:Header><SOAP-ENV:Body><AccountFindProofDetailsRequest xmlns="http://gbs.baml.com/data/OpenGBSAccountFindProofDetailsV001"><InputHeader xmlns="http://gbs.baml.com/data/OpenGBSHeaderV001"><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>
DMCA.com