如何使用SAX随时停止解析xml文档? [英] How to stop parsing xml document with SAX at any time?

查看:182
本文介绍了如何使用SAX随时停止解析xml文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Sax解析一个大的xml文档,我想在某些条件建立时停止解析文档?怎么办?

I parse a big xml document with Sax, I want to stop parsing the document when some condition establish? How to do?

推荐答案

创建一个SAXException的特化并抛出它(你不必创建自己的专业化但是它意味着您可以自己专门捕获并将其他SAXExceptions视为实际错误。)

Create a specialization of a SAXException and throw it (you don't have to create your own specialization but it means you can specifically catch it yourself and treat other SAXExceptions as actual errors).

public class MySAXTerminatorException extends SAXException {
    ...
}

public void startElement (String namespaceUri, String localName,
                           String qualifiedName, Attributes attributes)
                        throws SAXException {
    if (someConditionOrOther) {
        throw new MySAXTerminatorException();
    }
    ...
}

这篇关于如何使用SAX随时停止解析xml文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆