Share on Google+Share on Google+

XML Well-Formed-ness

In this section, you will learn to check the well-formed-ness of a XML using the DOM interface.

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 video insturction "How to check well formed XML in Java?":

Here is the XML File: 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 Java File:

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

public class DOMParserCheck {
  static public void main(String[] arg){
  BufferedReader bf = new BufferedReader(
new InputStreamReader(;
  System.out.print("Enter File name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  try {
  // Create a new factory to create parsers 
  DocumentBuilderFactory dBF = 
  // 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.out.print("File not found!");
  catch(IOException io){

Download this example.

Output of program:


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

Download source code of the project in Eclipse Project format



Posted on: June 4, 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-ness   View All Comments

Post your Comment

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