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 Error checker and locater (DOM)

                         

In this section, you will learn to check and locate (line and column number) an error in your XML document using the DOM APIs.  The XML document follows some rules to check its syntax. 

Description of program:

This program takes a XML file on the console and it checks whether the given file exists or not. If it exists then it parses the file using the parse() method. DocumentBuilderFactory and DocumentBuilder classes are needed to get a dom parser. All the  parsers have inbuilt capability to verify the well-formed ness of a xml file . No you need not to add any extra method for checking wellformed ness .If the xml file is wellformed , the parsing of the xml document get successfully completed , and the program displays a message like "Employee-Detail.xml is well-formed!". Otherwise, it displays an error and exact error location (line and column number). 

Here is the XML File: Employee-Detail1.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-Detail>

Here is the Java File: DOMLocateError.java

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

public class DOMLocateError{
  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()){
        // Create a new factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Use the factory to create builder document.
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        System.out.println(xmlFile + " is well-formed!");
      }
      else{
        System.out.print("File not found!");
      }
    }
    catch (SAXParseException e) {
      System.out.println("type" ": " + e.getMessage()+"\n");
      System.out.println("Line " + e.getLineNumber() " Column "
                                               + e.getColumnNumber
());
    }
    catch (SAXException e) {
      System.err.println(e);
      System.exit(1);
    }
    catch (ParserConfigurationException e) {
      System.err.println(e);
      System.exit(1);
    }
    catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }
  }
}

Download this example. 

Output of this program:

C:\vinod\xml>javac DOMLocateError.java

C:\vinod\xml>java DOMLocateError
Enter File name: Employee-Detail1.xml
[Fatal Error] Employee-Detail1.xml:9:1: The end-tag for element type "Employee"
must end with a '>' delimiter.
type: The end-tag for element type "Employee" must end with a '>' delimiter.

Line 9 Column 1

                         

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:

 

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.