Android SAXParser解析本地存储的xml文件

时间:2013-02-25 20:24:03

标签: java android xml-parsing local-storage sax

我正在使用SAXparse在线存储XML File。然后填充自定义GridView 这工作正常。 现在我想在Application Start下载XML文件到local storage,然后再解析它。

解析:

class loadingTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            gridView.setAdapter(new LkwAdapter(Lkw.this,itemList));
            showProgress.dismiss();
        }

        @Override
        protected String doInBackground(String... strings) {

            try{
                URL contURL = new URL(rUrl);
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
                LkwParseHandler handler = new LkwParseHandler();
                xr.setContentHandler(handler);
                xr.parse(new InputSource(contURL.openStream()));//THIS WORKS
                //xr.parse(new InputSource(new FileInputStream("test.xml")));
                //THIS IS ONE OF MY MANY TRIES, what InputSource do i use?

                itemList = handler.getLkwItems();

            }catch (Exception e){
                e.printStackTrace();
            }
            return "";
        }
    }

这适用于在线文件!! 然后,当我第一次保存文件并尝试使用它时,GridView保持空白!

保存文件(在启动画面的线程中)

InputStream input = null;
FileOutputStream output = null;
try {
   URL url = new URL("http://test.com/test.xml");
   input = url.openConnection().getInputStream();
   output = openFileOutput("test.xml", Context.MODE_PRIVATE);

   int read;
   byte[] data = new byte[1024];
   while ((read = input.read(data)) != -1)
       output.write(data, 0, read);

   output.close();
   input.close();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
} finally {
    Intent i = new Intent(SplashScreen.this, MainMenu.class);
   }

   startActivity(i);
   finish();
}

我首先保存正确吗?我错过了什么?

在这个问题中,您可以看到我正在尝试做什么Android Data handling and XML version control concept 最后,我想要将URLFilePath传递给AsyncTask

1 个答案:

答案 0 :(得分:0)

因为没有人看过这个问题(这里的交易是什么?)我在下班后使用了它:

...
FileInputStream fis =  openFileInput("test.xml");
InputStreamReader in = new InputStreamReader(fis);

xr.parse(new InputSource(in));
...