我有一个SAX
DefaultHandler来解析一个InputStream。我不知道XML中有多少元素,因此我无法在endElement
或simmilar上计算它们。我做知道InputStream
的字节长度(从http头读取)但我找不到一种方法来获取解析过程的当前字节进度。
有没有办法获得解析过程的当前进度(即位处理)?
这就是调用DefaultHandler
的方式:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputStream, myDefaultHandler);
答案 0 :(得分:3)
对于仍在寻找答案的人, 尝试在派生的FilterInputStream的构造函数中传递ProgressBar:
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);
}
}
我在我的
中的AsyncTask中使用它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);
答案 1 :(得分:1)
从lx222答案扩展,我实现了AsyncTask,ProgressDialog。迟到但我希望它对某些人有用: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);
}
}
}
答案 2 :(得分:0)
您可以通过编写FilterInputStream来包装现有的inputStream来完成此操作。在过滤器的read()
方法中,递增计数器,并提供一个getter,以便其他内容可以跟踪当前计数。
由于解析器可以提前读取,这将是近似的,但它可能是你能做的最好的。