Android:SAX解析器进度监控
我有一个 SAX
DefaultHandler 解析输入流。我不知道 XML 中有多少个元素,因此无法根据 endElement
或 simmilar 来计算它们。我确实知道InputStream
的字节长度(从http标头读取),但我找不到获取解析过程当前字节进度的方法。
有没有办法获取解析过程的当前进度(即处理的位)?
这是 DefaultHandler
的调用方式:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputStream, myDefaultHandler);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于仍在寻找答案的人来说,
尝试在派生的 FilterInputStream 的构造函数中传递 ProgressBar:
ProgressFilterInputStream.java
的 AsyncTask 中使用了它
我在MyActivity.java
For anyone who is still searching for the answer,
try to pass the ProgressBar in the constructor of your derived FilterInputStream:
ProgressFilterInputStream.java
I used it in an AsyncTask in my
MyActivity.java
从 lx222 答案扩展,我实现了 AsyncTask、ProgressDialog。迟到了,但我希望它对某些人有用:D
Extended from lx222 answer, I implemented for AsyncTask, ProgressDialog. Late but I hope it may useful with some one :D
您可以通过编写 FilterInputStream 来完成此操作包装现有的输入流。在过滤器的 read() 方法中,增加一个计数器,并提供一个 getter,以便其他东西可以跟踪当前计数。
由于解析器可能会提前读取,因此这将是近似值,但这可能是您能做的最好的事情。
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.