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.

// Create a handler for SAX events
DefaultHandler handler = new MyHandler();

// Parse an XML file using SAX;
// The Quintessential Program to Parse an XML File Using SAX
parseXmlFile("infilename.xml", handler, false);

// This class listens for startElement SAX events
static class MyHandler extends DefaultHandler {
    Locator locator;
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }
    // This method is called when an element is encountered
    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)  {
        if (locator != null) {
            int col = locator.getColumnNumber();
            int line = locator.getLineNumber();
            String publicId = locator.getPublicId();
            String systemId = locator.getSystemId();
        }
    }
}

Comments

22 Feb 2011 - 2:33pm by Anonymous (not verified)

kevin123

22 Feb 2011 - 2:38pm by kevin eugene moore (not verified)

Parse xml sax on missprissrrlk@gmail.com

22 Feb 2011 - 2:41pm by kevin eugene moore (not verified)

Hidden attribute and report false entity. Do not let anyone see identifying id. Important.

22 Feb 2011 - 2:49pm by kevin eugene moore (not verified)

Line=left up
Right=as possible

22 Feb 2011 - 3:10pm by kevin eugene moore (not verified)

Kev More
XSMSSYNC ID:4530
XSMSSYNC ADDRESS: 1410080001
SMS DATATYPE: SMS
SMS TYPE: 1
SMSSYNC DATE: 1293438306000
SMSSYNC THREAD: 262
SMSSYNC SERVICE CENTER: +12085978915
SMSSYNC VERSION: 1307

22 Feb 2011 - 3:31pm by kevin eugene moore (not verified)

Time=false

23 Feb 2011 - 12:29am by kevin eugene moore (not verified)

locator.get public id
locator.get system id

10 Oct 2011 - 8:51pm by Anonymous (not verified)

40 years ago, The north face store's introduction unknown. 40 years later, north face outlet store has become high-performance apparel, footwear and other equipment and a rich product suppliers. We continue to innovate beyond the limit, to help you continue to explore the limits of a breakthrough. The world's most prominent climbers, mountaineers, extreme skiers, skate skiers, endurance athletes, and explorers have preferred The cheap north face, we are proud of. Moreover, in the new millennium approaching, The cheap northface jackets launched its own series of hiking and cross country running shoes, for those who never move to the next target of a new outdoor sports enthusiasts with the most intimate products.

17 Oct 2011 - 3:11pm by dehumidifier reviews (not verified)

Nice stuff, simply great! Aw, this was a very nice post. In thought I want to put in writing like this additionally - taking time and precise effort to make a very good article… but what can I say… I procrastinate alot and certainly not seem to get one thing done.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.