📄 e550. determining the parsing location of an xml sax parser.txt
字号:
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;
// e517 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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -