Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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>

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.