关于handle中重写startElement()方法中的参数问题
startElement(String uri, String localName, String qName,
Attributes attributes)中的参数在哪里传入handle对象的呀?主函数里新建了一个handle对象,也没传参数呀?尤其这个localName参数,都没有见到呀,或者是parse()函数传来的?
startElement(String uri, String localName, String qName,
Attributes attributes)中的参数在哪里传入handle对象的呀?主函数里新建了一个handle对象,也没传参数呀?尤其这个localName参数,都没有见到呀,或者是parse()函数传来的?
2017-06-01
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
SaxParserHandler saxParserHandler = new SaxParserHandler();
saxParser.parse("books.xml", saxParserHandler);
//parse()
public void parse(String uri, DefaultHandler dh)
throws SAXException, IOException {
if (uri == null) {
throw new IllegalArgumentException("uri cannot be null");
}
InputSource input = new InputSource(uri);
this.parse(input, dh);//------------执行到此处
}
public void parse(InputSource is, DefaultHandler dh)
throws SAXException, IOException {
if (is == null) {
throw new IllegalArgumentException("InputSource cannot be null");
}
XMLReader reader = this.getXMLReader();
if (dh != null) {
reader.setContentHandler(dh);
reader.setEntityResolver(dh);
reader.setErrorHandler(dh);
reader.setDTDHandler(dh);
}
reader.parse(is);//----------
}
public void parse (InputSource input)
throws IOException, SAXException;
//----------
/**
* Receive notification of the start of an element.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the start of
* each element (such as allocating a new tree node or writing
* output to a file).</p>
*
* @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace
* processing is not being performed.
* @param localName The local name (without prefix), or the
* empty string if Namespace processing is not being
* performed.
* @param qName The qualified name (with prefix), or the
* empty string if qualified names are not available.
* @param attributes The attributes attached to the element. If
* there are no attributes, it shall be an empty
* Attributes object.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#startElement
*/
public void startElement (String uri, String localName,
String qName, Attributes attributes)
throws SAXException
{
// no op
}
举报