Search code examples
androidxmlreadfilesettext

How to read XML file and pass to setText as String with all tags and white-characters [Android]


I want to read XML file, save it as String and pass to setText. I don't want to parse it but see it on my smartphone screen with all tags and white-characters, eg.

<a>
  <b>some text</b>
</a>

not:

some text

How to do it?


Solution

  • FYI, this is how I solve my problem:

        public String readXML() {
    
            String line;
            StringBuilder total = new StringBuilder();
    
            try {
                InputStream is = activity.getAssets().open("subjects.xml");
    
                BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                total = new StringBuilder();
    
                while ((line = r.readLine()) != null) {
                    total.append(line + "\n");
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return total.toString();
        }