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!

Locating the Notified Events

                         

In this section, you will learn to locate (line and column number) the generated events  while parsing a XML file using SAX APIs. 

Description of program:

This program takes a XML file at the console. Before parsing, the setContentHandler() method invokes the XMLReader object and it takes DefaultHandler object. The setContentHandler method allows an application to register a content event handler. The parser starts parsing a XML document, as soon as  it encounters the root tag at at Line no: 1 Column no: 1, the startDocument() method is called and the program displays "startDocument() method called at Line no: 1 Column no: 1" at the console. When parser encounters the first elements, it calls  the startElement() method and it displays "startElement() method called at Line no: 1 Column no: 19. Similarly, the parser calls characters(), endElement() and endDocument() methods. The parser displays appropriate messages according to the events generated while parsing a xml document. 

Here is the XML File: sample.xml

<Employee-detail >
    <Employee name="Amit" >
        <address>My address</address>
     </Employee>
     <issued-items> 
        <item code="111" type="CD" label=" music" />
        <item code="222" type="DVD" label=" video"/>
     </issued-items>
</Employee-detail>

Here is Java File: WorkingSAXParserLocato.java

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.Locator;

public class WorkingSAXParserLocator extends DefaultHandler {
  private Locator locator;
  public static void main(String[] args) {
    try {
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter xml file name: ");
      String xmlFile = bf.readLine();
      File file = new File(xmlFile);
      if (file.exists()){
        XMLReader parser = XMLReaderFactory.createXMLReader();
        DefaultHandler handler = new WorkingSAXParserLocator();   
        parser.setContentHandler(handler);
        //parse xml file
        parser.parse(xmlFile);
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }
  public void setDocumentLocator(Locator locator) {
    this.locator = locator;
  }
  //All methods positions
  private void printLocation(String location) { 
    int line = locator.getLineNumber();
    int column = locator.getColumnNumber();
    System.out.println(location + "  Line no: " + line + 
                                                  
"\tColumn no: " + column);
  }
  //Document start
  public void startDocument() { 
    printLocation("startDocument() method called at");
  }
  //Document end
  public void endDocument() { 
    printLocation("endDocument() method called at");
  }
  //Element start
  public void startElement(String namespaceURI, String localName, 
    String qualifiedName, Attributes atts) {
    printLocation("startElement() method called at ");
  }
  //Element end
  public void endElement(String namespaceURI, String localName, 
    String qualifiedName) {
    printLocation("endElement() method called at \t");
  }
  //characters
  public void characters(char[] text, int start, int length) {
    printLocation("characters() method called at \t")
  }
}

Download this example.

Output of this program:

C:\vinod\xml\sax1>javac WorkingSAXParserLocator.java

C:\vinod\xml\sax1>java WorkingSAXParserLocator
Enter xml file name: sample.xml
startDocument() method called at  Line no: 1    Column no: 1
startElement() method called at     Line no: 1    Column no: 19
characters() method called at        Line no: 2    Column no: 2
startElement() method called at     Line no: 2    Column no: 25
characters() method called at        Line no: 3    Column no: 3
startElement() method called at     Line no: 3    Column no: 12
characters() method called at        Line no: 3    Column no: 22
endElement() method called at      Line no: 3    Column no: 32
characters() method called at        Line no: 4    Column no: 2
endElement() method called at      Line no: 4    Column no: 13
characters() method called at        Line no: 5    Column no: 2
startElement() method called at     Line no: 5    Column no: 16
characters() method called at        Line no: 5    Column no: 17
characters() method called at        Line no: 6    Column no: 3
startElement() method called at     Line no: 6    Column no: 47
endElement() method called at      Line no: 6    Column no: 47
characters() method called at        Line no: 7    Column no: 3
startElement() method called at     Line no: 7    Column no: 47
endElement() method called at      Line no: 7    Column no: 47
characters() method called at        Line no: 8    Column no: 2
endElement() method called at      Line no: 8    Column no: 17
characters() method called at        Line no: 9    Column no: 1
endElement() method called at      Line no: 9    Column no: 19
endDocument() method called at   Line no: 9    Column no: 19

                         

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.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
  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.