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.

Locating the Notified Events

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