问答详情
源自:3-2 使用 SAX 解析 XML 文件的节点属性

关于handle中重写startElement()方法中的参数问题

startElement(String uri, String localName, String qName,

Attributes attributes)中的参数在哪里传入handle对象的呀?主函数里新建了一个handle对象,也没传参数呀?尤其这个localName参数,都没有见到呀,或者是parse()函数传来的?


提问者:0110号建筑师 2017-06-01 22:06

个回答

  • 君士坦丁11
    2017-06-03 16:42:04
    已采纳

    我感觉这个startElement方法应该是JAVA官方类的源代码中的方法,只有用法。当你主函数调用parse(uri,dh)时,parse方法就已经把xml文件进行解析,并且获取了你问题里的参数,这都是官方类的操作,我们看不到的。

  • 政政0213
    2018-10-26 16:31:17

    请注意看参数

  • 政政0213
    2018-10-26 16:30:37

    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;

    //----------

    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

        }