问题
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);
回答1:
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.
回答2:
For anyone who is still searching for the answer, try to pass the ProgressBar in the constructor of your derived FilterInputStream:
ProgressFilterInputStream.java
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import android.widget.ProgressBar;
import android.util.Log;
public class ProgressFilterInputStream extends FilterInputStream {
int counter = 0;
ProgressBar progressBar;
public ProgressFilterInputStream(InputStream in, ProgressBar progressBar) {
super(in);
this.progressBar = progressBar;
this.progressBar.setMax(69); // SET MAX ACCORDIN TO THE DDMS LOGGING MESSAGE
}
@Override
public int read(byte[] buffer, int offset, int count) throws IOException {
progressBar.setProgress(counter++);
Log.i("PFIS" , "Counter: " + counter);
return super.read(buffer, offset, count);
}
}
I used it in an AsyncTask in my
MyActivity.java
final ProgressBar bar_progress = (ProgressBar) findViewById(R.id.bar_progress);
ProgressFilterInputStream pfis = new ProgressFilterInputStream(getAssets().open("DATA.xml") , bar_progress );
Reader reader = new InputStreamReader(pfis, "UTF-8");
InputSource is = new InputSource(reader);
// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// instantiate our handler
SaxHandler myHandler = new SaxHandler();
// assign our handler
xmlreader.setContentHandler(myHandler);
// perform the synchronous parse
xmlreader.parse(is);
回答3:
Extended from lx222 answer, I implemented for AsyncTask, ProgressDialog. Late but I hope it may useful with some one :D
public class OpenXMLFileOperator extends AsyncTask<Void, Integer, Void>{
private ProgressDialog progressDialog;
private String filePath;
private Context context;
public OpenFileOperator(Context context,String filePath){
this.context = context;
this.filePath = filePath;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
if (filePath != null) {
File file = new File(filePath);
if (file.exists()) {
InputStream stream = new FileInputStream(file);
ProgressFilterInputStream pfis = new ProgressFilterInputStream(stream,file.length());
Reader reader = new InputStreamReader(pfis, "UTF-8");
YourXMLHandle yourHandle = new YourXMLHandle();
android.util.Xml.parse(reader,yourHandle);
stream.close();
reader.close();
pfis.close();
}
}
}catch (Exception e) {
if (BuildConfig.DEBUG) Log.e(TAG, "Exception", e);
}
return null;
}
@Override
protected void onPreExecute() {
try{
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading file");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}catch(Exception e){
if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
}
super.onPreExecute();
}
@Override
protected void onCancelled() {
try{
if (progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
}catch(Exception e){
if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
}
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
try{
if (progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
}catch(Exception e){
if (BuildConfig.DEBUG) Log.e(TAG,"onPostExecute",e);
}
//TODO: to do something after file loaded
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
int progress = values[0];
if (progressDialog!=null) {
progressDialog.setProgress(progress);
}
super.onProgressUpdate(values);
}
private class ProgressFilterInputStream extends FilterInputStream {
private int counter = 0;
private long fileLength;
private int maximum = 100;
public ProgressFilterInputStream(InputStream in, long fileLength) {
super(in);
this.fileLength = fileLength;
}
@Override
public int read(byte[] buffer, int offset, int count) throws IOException {
if (counter==0){
maximum = (int)(fileLength/buffer.length);
}
publishProgress((counter++)*100/maximum);
return super.read(buffer, offset, count);
}
}
}
来源:https://stackoverflow.com/questions/5015856/android-sax-parser-progress-monitoring