read XML from Resource - Android XML

Android examples for XML:XML Resource

Description

read XML from Resource

Demo Code


//package com.java2s;
import android.content.res.Resources;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String readXML(Resources res, String file) {
        BufferedReader in = null;
        try {//  w  ww  . j  a  va 2  s  . co  m
            in = new BufferedReader(new InputStreamReader(res.getAssets()
                    .open(file)));
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = in.readLine()) != null)
                buffer.append(line).append('\n');
            return buffer.toString();
        } catch (IOException e) {
            return "";
        } finally {
            closeStream(in);
        }
    }

    private static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }
}

Related Tutorials