簡體   English   中英

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

[英]Android SAXParser parse local stored xml file

我正在使用SAX parse在線存儲的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數據處理和XML版本控制概念最后,我想將URLFilePath傳遞給AsyncTask

由於沒有人甚至沒有看過這個問題(這里有什么關系?),我幾個小時后就開始使用:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM