Name
The Locator Interface
Synopsis
Unlike most other interfaces in the org.xml.sax
package, the Locator
interface does not have to be
implemented. Instead, the parser has the option to provide an
implementation. If it does so, it passes its implementation to the
setDocumentLocator( )
method in
the ContentHandler
instance
before it calls startDocument(
)
. You can save a reference to this object in a field in
your ContentHandler
class, like
this:
private Locator locator; public void setDocumentLocator(Locator locator) { this.locator = locator; }
Once you’ve found the locator, you can then use it inside
any other ContentHandler
method, such as startElement( )
or characters( )
, to determine
in exactly which document and at which line and column the event
took place. For instance, the locator allows you to determine that
a particular start-tag began on the third column of the document’s
seventeenth line at the URL
http://www.slashdot.org/slashdot.xml:
package org.xml.sax; public interfaceLocator
{ public StringgetPublicId
( ); public StringgetSystemId
( ); public intgetLineNumber
( ); public intgetColumnNumber
( ); }
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.