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

查看:58
本文介绍了如何随时停止使用 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天全站免登陆