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
 
 
Hot Web Programming Job

 

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 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" ?>
<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>

<Employe>
<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: LocateError.java

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.io.*;

public class LocateError{

  public static void main(String[] argsthrows IOException{
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter XML file name:");
    String xmlFile = bf.readLine();
    LocateError par = new LocateError(xmlFile);
  }
  
  public LocateError(String str){
    try{
      File file = new File(str);
      if (file.exists()){
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.parse(str);
        System.out.println(str + " is well-formed!");
      }
      else{
        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){
      System.out.println(io.getMessage());
    }
  }
}

Download this example.

Output of program:

C:\vinod\xml>javac LocateError.java

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

                         

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:

Audio Version
Reload Image
 

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.