SAX中止解析XML方法

来源:互联网 发布:印象笔记导出为知笔记 编辑:程序博客网 时间:2024/06/05 05:41

思路:定义一个特定的异常类来标识,直接抛异常。在调用saxparser.parse的地方捕获这个异常。

 

    @Override    public void endElement(String uri, String localName, String name) throws SAXException {        if("aaaa".equals(name)){   // 抛出异常,中断解析            throw new SAXException("找到指定信息");            }else                //继续解析        }       }


捕获异常:

    public boolean isExisted(String id){        try {            saxParser.parse(IN, ieuh);        } catch (SAXException e) {            System.out.println(e.getMessage());            return true;        } catch (IOException e) {            e.printStackTrace();        }        return false;    }