安卓:SAX解析器进度监控进度、SAX

2023-09-07 16:49:54 作者:我想变成天边那朵白云,用尽整日晴天,只从左边,移到右边。今天

我有一个 SAX 的 DefaultHandler的它解析一个InputStream。我不知道有多少元素在XML,所以我不能指望他们对的endElement 或simmilar。 我做知道的字节长度的InputStream (从HTTP头中读出),但我无法找到一个方法来获得当前的按字节的进展的解析过程。

有没有办法让目前的进展(如位处理)的解析过程呢?

这是怎么了的DefaultHandler 被调用:

 的SAXParserFactory厂= SAXParserFactory.newInstance();
的SAXParser解析器= factory.newSAXParser();
parser.parse(InputStream的,myDefaultHandler);
 

解决方案 鲁班浏览器下载 Luban Explorer 鲁班浏览器官方8.0下载 浏览器 下载之家

您可以通过编写的 FilterInputStream中的来包装现有的InputStream。在过滤器的阅读()方法,增加一个计数器,并提供一个getter,这样别的东西可以跟踪当前计数。

由于分析器可以预读,这将是近似的,但它可能是最好的,你可以做的。

I have a SAX DefaultHandler which parses an InputStream. I don't know how many elements are in the XML so I can't count them on endElement or simmilar. I do know the byte length of the InputStream (read from the http header) but I can't find a method to get the current bytewise progress of the parsing process.

Is there a way to get the current progress (i.e. bits processed) of the parsing process?

This is how the DefaultHandler gets called:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputStream, myDefaultHandler);

解决方案

You can do this by writing a FilterInputStream to wrap the existing inputStream. In the read() method of your filter, increment a counter, and provide a getter so that something else can track the current count.

Since the parser may read ahead, this will be approximate, but it's probably the best you can do.