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 Well-Formed-ness

                         

In this section, you will learn to check the well-formed-ness  of a XML using the DOM interface. A  well-formed  XML  document must follow the xml syntax rules.

Description of program:

For checking the "well-formedness" of  a XML document you should use the given example. The DOM parser parsers (parse()) the XML document using the DocumentBuilder and DocumentBuilderFactory. Whenever the XML document is well-formed, it shows a message "Employee-Detail.xml is well-formed". Otherwise it displays "Employee-Detail.xml isn't well-formed.". 

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: DOMParserCheck.java

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMParserCheck {
  static public void main(String[] arg){
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter File name: ");
      String xmlFile = bf.readLine();
      File file = new File(xmlFile);
      if(file.exists()){
        try {
          // Create a new factory to create parsers 
          DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
          // Use the factory to create a parser (builder) and use
          // it to parse the document.
          DocumentBuilder builder = dBF.newDocumentBuilder();
          // builder.setErrorHandler(new MyErrorHandler());
          InputSource is = new InputSource(xmlFile);
          Document doc = builder.parse(is);
          System.out.println(xmlFile + " is well-formed!");
        }
        catch (Exception e) {
          System.out.println(xmlFile + " isn't well-formed!");
          System.exit(1);
        }
      }
      else{
        System.out.print("File not found!");
      }
    }
    catch(IOException io){
      io.printStackTrace();
    }
  }
}

Download this example.

Output of program:

C:\vinod\xml>javac DOMParserCheck.java

C:\vinod\xml>java DOMParserCheck
Enter File name: Employee-Detail.xml
Employee-Detail.xml is well-formed!

                         

Ask programming questions?

 

 

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

Current Comments

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

catch(IOException io){
io.printStackTrace();
}
Unreachable catch Clause

Posted by Abinash Mishra on Sunday, 03.1.09 @ 04:46am | #85343

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
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.

[an error occurred while processing this directive]