關(guān)于handle中重寫startElement()方法中的參數(shù)問題
startElement(String uri, String localName, String qName,
Attributes attributes)中的參數(shù)在哪里傳入handle對(duì)象的呀?主函數(shù)里新建了一個(gè)handle對(duì)象,也沒傳參數(shù)呀?尤其這個(gè)localName參數(shù),都沒有見到呀,或者是parse()函數(shù)傳來的?
2017-06-03
我感覺這個(gè)startElement方法應(yīng)該是JAVA官方類的源代碼中的方法,只有用法。當(dāng)你主函數(shù)調(diào)用parse(uri,dh)時(shí),parse方法就已經(jīng)把xml文件進(jìn)行解析,并且獲取了你問題里的參數(shù),這都是官方類的操作,我們看不到的。
2018-10-26
請(qǐng)注意看參數(shù)
2018-10-26
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);//------------執(zhí)行到此處
? ? }
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;
//----------
void org.xml.sax.XMLReader
?/**
? ? ?* 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
? ? }