SAX解析器跳过一些不被解析的元素? [英] SAX parser to skip some elements which are not to be parsed?

查看:123
本文介绍了SAX解析器跳过一些不被解析的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个像

<root>
  <transaction ts="1">
    <abc><def></def></abc>
  </transaction>
  <transaction ts="2">
    <abc><def></def></abc>
  </transaction>
</root>

所以,我有一个条件,如果ts =2,那么做点什么......现在问题是,当它发现ts =1时,它仍然会扫描标签< ABC>< def>然后达到<事务ts =2>

So, I have a condition which says if ts="2" then do something ... Now the problem is when it finds ts="1" it still scans through tags < abc>< def> and then reaches < transaction ts="2">

当条件与解析中断不匹配并直接查找下一个事务标记时,有没有办法?

Is there a way when the condition doesn`t match the parsing breaks and look for the next transaction tag directly?

推荐答案

SAX解析器必须扫描所有子树(例如< abc>< def>< / def>< / abc >)知道下一个元素的起始位置。没办法绕过它,这也是你无法为单个XML文档并行化XML Parser的原因。

A SAX parser must scan thru all sub trees (like your "< abc>< def>< /def>< /abc>") to know where the next element starts. No way to get around it, which is also the reason why you cannot parallelize a XML Parser for a single XML document.

只有两种调整方式我能想到在你的情况下:

The only two ways of tuning I can think of in your case:

1)如果要解析许多XML文档,可以在自己的线程中为每个文档运行一个Parser。这至少会使整个工作并行化,并利用你可用的所有CPU和核心。

1) If you have many XML documents to parse, you can run one Parser for each document in its own thread. This would at least parallelize the overall work and utilize all CPU's and Cores you have available.

2)如果你只需要阅读一定的条件(就像你提到的那样) < transaction ts =2>)一旦达到该条件,您就可以跳过解析。如果跳过解析器会有所帮助,那么通过抛出异常就可以了。

2) If you just need to read up to a certain condition (like you mentioned < transaction ts="2">) you can skip parsing as soon as that condition is reached. If skipping the parser would help, the way to this is by throwing an Exception.

你的 startElement 的实现 ContentHandler 将如下所示:

Your implementation of startElement within the ContentHandler would look like this:

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    if(atts == null) return;
        if(localName.equals("transaction") && "2".equals(atts.getValue("ts"))) {
            // TODO: Whatever should happen when condition is reached
            throw new SAXException("Condition reached. Just skip rest of parsing");
        }
    }

这篇关于SAX解析器跳过一些不被解析的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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