【问题标题】:Java Sax Parser - stop processing in EndElementListenerJava Sax Parser - 在 EndElementListener 中停止处理
【发布时间】:2012-01-10 01:04:25
【问题描述】:

我正在使用 android 的 SAX 解析器,我希望在读取 N 个元素后停止处理。有些提要非常大,可能需要一段时间才能完成。如果某个元素的 EndElementListener 中满足某些条件,如何停止解析?这是我目前的听众

chanItem.setEndElementListener(new EndElementListener()  {
    public void end() {
        _items.add(_item);
        if (++_currentItem == _maxElements) {
                //BREAK OUT HERE
        }
    }
});

我尝试在 end() 中抛出异常,但 EndElementListener 不允许抛出任何异常。 非常感谢您的指导。

【问题讨论】:

  • 它实际上不是 Nick - 如果你看一下你会发现我的有点不同,因为它使用了 android.sax 解析器。我不能在 EndElementListener 中抛出异常,因为接口不支持它。
  • 啊,我的错,我没有看正确的。那时我不确定,很抱歉造成混乱。尽管没有标记答案,但已在另一个问题上对此进行了讨论。 stackoverflow.com/q/3508635/965648
  • 这让尼克很困惑,根本不用担心......我只是更喜欢 android.sax 解析器使用 ElementListeners 的方式,所以我想继续使用它,假设我可以得到这个工作..

标签: java android xml sax saxparser


【解决方案1】:

定义一个名为“SaxBreakOutException”的新的未经检查的异常:

class SaxBreakOutException extends RuntimeException {
}

把这个扔给你的听众:

chanItem.setEndElementListener(new EndElementListener()  {
    public void end() {
        _items.add(_item);
        if (++_currentItem == _maxElements) {
            throw new SaxBreakOutException();
        }
    }
});

并在调用 XmLReader.parse() 的代码中捕获它:

reader.setContentHandler(handler);
try {
    reader.parse(new InputSource(new StringReader(xml)));
} catch (SaxBreakOutException allDone) {
}

或者,切换到 Android 的 XmlPullParser,它不需要提前爆发异常。

【讨论】:

  • 太棒了!我自己也在尝试这种方法,但扩展的是 SaxException 而不是 RuntimeException。谢谢杰西..
【解决方案2】:

“Jesse Wilson”的答案很好,但就我而言,我必须保留返回我的 parse() 方法的 de 值。

List<Article> ArticleNews = parser.parse();

所以我添加了一个布尔值;

int countArts;
boolean stopParse;

当我不得不停止我的 parser() 方法时,我用

消耗所有的侦听器
if(stopParse)return;

一个例子:

public List<Article> parse(){
        final List<Article> myArticles= new ArrayList<Article>();

        final RootElement root = new RootElement("articles");
        Element nota = root.getChild("article");


        nota.setStartElementListener(new StartElementListener(){
            @Override
            public void start(Attributes attributes) {
                Log.i("----------------------------------------", "START Article!\n");
            }
        });

        nota.setEndElementListener(new EndElementListener(){
            public void end() {  

                if(countArts>9){    //only 10 articles!.
                    stopParse=true;
                }               
                countArts++;
                Log.i("----------------------------------------", "END Article!\n");

            }
        });


        nota.getChild(ID).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                if(stopParse)return;
                if(Utilities.isNumeric(body)) {
                    idA = body;
                }
            }
        });
        nota.getChild(CATEGORY).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                if(stopParse)return;
                categoriaA = body;
            }
        });

        nota.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                if(stopParse)return;
                tituloA = body;
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        }  catch (Exception e) {
            Log.e(" *** Exception Xml.parse() ", e.getMessag
        }
        return myArticles;
    }

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多