Share on Google+Share on Google+

XML Well-Formed Verifier

In this section we are going to develop a simple java program that determines whether a XML document is well-formed or not.

XML Well-Formed Verifier


In this section we are going to develop a simple java program that determines whether a  XML document is well-formed or not.

Description of the program:

In the following program, you will learn to parse a xml document and determine whether it is well-formed or not.    Develop a java file ( that uses a  xml file to parse and check its well-formedness. Initially the program checks that the given file exists or not by using exists() method. If the file exists then it will parse the xml file with the help of parse() method.

The XMLReaderFactory helps in creating an XML reader which parses  xml document using the appropriate callbacks. And it determines that the parsed xml is well-formed or not. If xml document is will-formed, it will display a message "Employee-Detail.xml is well-formed!" Otherwise prints "Employee-Detail.xml isn't well-formed!". If you enter a  file that doesn't exist it will show "File not found: Employee-Detail.xml".

Here is the XML File to be parsed: Employee-Detail.xml

<?xml version = "1.0" ?>

<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>

<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>


Here is the code of Java File:

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SAXParserCheck{

  public static void main(String[] argsthrows IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  System.out.print("Enter XML file name:");
  String xmlFile = bf.readLine();
  SAXParserCheck par = new SAXParserCheck(xmlFile);
  public SAXParserCheck(String str){
  File file = new File(str);
  if (file.exists()){
  XMLReader reader = XMLReaderFactory.createXMLReader();
  System.out.println(str + " is well-formed!");
  System.out.println("File not found: " + str);
  catch (SAXException sax){
  System.out.println(str + " isn't well-formed");
  catch (IOException io){

Download this example 

Output of program:


C:\vinod\xml\comXML>java SAXParserCheck
Enter XML file name:Employee-Detail.xml
Employee-Detail.xml is well-formed!




Posted on: May 25, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: XML Well-Formed Verifier   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image