Determining The Parsing Location Of An XML SAX Parser

 
 
To get the current URI and location of a SAX parser during parsing,you need to obtain a Locator object. This object can be obtained by installing a ContentHandler and overriding the setDocumentLocator() method. The SAX parser will call this method and deliver the Locator object that you can use anytime it delivers an event (i.e.,invokes a callback method in a handler). This method will be called before any other ContentHandler method. Note: Not all SAX parser support a Locator. If it doesn't,the setDocumentLocator() method will not be called. This example captures the Locator object and uses it whenever it gets a startElement event.
 
 
  1. // Create a handler for SAX events

  2.     DefaultHandler handler = new MyHandler();

  3.    

  4.     // Parse an XML file using SAX;

  5. parseXmlFile("infilename.xml",handler,false);

  6.    

  7.     // This class listens for startElement SAX events

  8.     static class MyHandler extends DefaultHandler {

  9.         Locator locator;

  10.         public void setDocumentLocator(Locator locator) {

  11.             this.locator = locator;

  12.         }

  13.         // This method is called when an element is encountered

  14.         public void startElement(String namespaceURI,String localName,String qName,Attributes atts)  {

  15.             if (locator != null) {

  16.                 int col = locator.getColumnNumber();

  17.                 int line = locator.getLineNumber();

  18.                 String publicId = locator.getPublicId();

  19.                 String systemId = locator.getSystemId();

  20.             }

  21.         }

  22.     }

  23.  

  24.  

  25.  
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 5th Apr 2007
  Rating : No Rating
  Accessed  :  5851
  Submitted by :  javabill
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available

    No comment available at the moment.Be the first one to make a comment

 
Related Java Source Codes
  • Spring
  • Wraps a string to a given number of characters using a string break character
  • Uppercase the first character of each word in a string
  • Make a string's first character uppercase
  • Strip whitespace (or other characters) from the beginning and end of a string

  • Copyright © 2013 VisualBuilder. All rights reserved