2016年11月13日日曜日

Android PULL解析XML文件

PULL解析XML文件

https://developer.android.com/training/basics/network-ops/xml.html
http://cdefgab.web.fc2.com/test.xml



public class MainActivity extends AppCompatActivity {
    private File mainfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

//得到XML文件并保存为文件
    public void getXML(View view) {
        chk c = new chk();
        c.execute("test");
    }

//PULL解析XML文件
    public void pullxml(View view) throws Exception {
        FileInputStream fileInputStream = this.openFileInput("xxxxx.xml");
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fileInputStream, "utf-8");
        HashMap<Integer, HashMap> data = new HashMap<Integer, HashMap>();
        HashMap<String, String> data2 = null;
        Integer con = new Integer(0);
        int type = parser.getEventType();
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if ("book".equals(parser.getName())) {
                        data2 = new HashMap<String, String>();
                        data2.put("id", parser.getAttributeValue(0));
                    }
                    if ("author".equals(parser.getName())) {
                        data2.put("author", parser.nextText());
                    }
                    if ("genre".equals(parser.getName())) {
                        data2.put("genre", parser.nextText());
                    }
                    if ("price".equals(parser.getName())) {
                        data2.put("price", parser.nextText());
                    }
                    if ("publish_date".equals(parser.getName())) {
                        data2.put("publish_date", parser.nextText());
                    }
                    if ("description".equals(parser.getName())) {
                        data2.put("description", parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    data.put(con, data2);
                    con++;
                    break;
                case XmlPullParser.END_DOCUMENT:
                    break;
            }
            type = parser.next();
        }
        System.out.println(data.get(0).toString());
    }

    public class chk extends AsyncTask<String, Integer, File> {
        @Override
        protected File doInBackground(String... params) {
            File file_path = null;
            String url_path = "http://cdefgab.web.fc2.com/test.xml";
            String f_name = "xxxxx.xml";
            GetXML getXML = new GetXML(MainActivity.this, url_path, f_name);
            try {
                file_path = getXML.tofile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return file_path;
        }

        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            mainfile = file;
        }
    }
}



//得到XML文件并保存为文件
public class GetXML {
    private Context context;
    private String url_path;
    private String f_name;
    private InputStream inputStream;
    private FileOutputStream fileOutputStream;

    public GetXML(Context context, String url_path, String f_name) {
        this.context = context;
        this.url_path = url_path;
        this.f_name = f_name;
    }

    public File tofile() throws IOException {
        URL url = new URL(url_path);
        fileOutputStream = context.openFileOutput(f_name, Context.MODE_PRIVATE);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpURLConnection.getInputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, len);
                fileOutputStream.flush();
            }
        }
        fileOutputStream.close();
        inputStream.close();
        return context.getFileStreamPath(f_name);
    }

}

0 件のコメント: