Share on Google+Share on Google+

XML Error Checking and Locating

In this section, you will learn how to check and locate an error in the XML document and error position.

XML Error Checking and Locating


In this section, you will learn how to check and locate an error in the XML document and error position. 

Description of program:

The following program helps you in checking and locating an error in XML document.

Implementing this program you need a XML document. When you run the following program then it asks a xml file name and checks it. If the given file exists, the SAX Parser parses the XML document and detects the error in it at the specified line and column by using the getLineNumber() and getColumnNumber() methods. The getLineNumber method detects line of error and the getColumnNumber method also detects column of error and prints the line and column. If no any error in XML  document, it will show a message "Employee-Detail.xml is well-formed". Whenever the given file doesn't exist, it displays "File not found!".

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 org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class LocateError{

  public static void main(String[] argsthrows IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  System.out.print("Enter XML file name:");
  String xmlFile = bf.readLine();
  LocateError par = new LocateError(xmlFile);
  public LocateError(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 (SAXParseException sax){
  System.out.println(str + " isn't well-formed");
  int line = sax.getLineNumber();
  int col = sax.getColumnNumber();
  System.out.println("Error is at line number "+ line + 
    and Column position "
+ col);
  catch (SAXException sax){}
  catch (IOException io){

Download this example.

Output of program:


C:\vinod\xml>java LocateError
Enter XML file name:Employee-Detail.xml
[Fatal Error] Employee-Detail.xml:19:11: The end-tag for element type "Employe"
must end with a '>' delimiter.
Employee-Detail.xml isn't well-formed
Error is at line number 19 and Column position 11



Posted on: May 27, 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 Error Checking and Locating  

Post your Comment

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