Home » Android » Android : Reading XML from local resource (for testing)

Android : Reading XML from local resource (for testing)

Posted by: admin June 15, 2020 Leave a comment


I’m writing an app which will read XML from a webservice (probably via kSOAP2). I’m fairly happy with SAX parsing, as I’ve done XML parsing iPhone apps.

Unfortunately the webservice isn’t public yet so for initial testing I have some files containing the XML I need to parse. In this early dev phase I just need to read the XML from the files and pass it into the XML parser

Xml.parse(this.testXML, root.getContentHandler());

How do I read the XML from a file/resource into a string to pass into this method. I want to crack on and test the parser, but this simple step is holding me up.


How to&Answers:

Create a raw folder under res

Put your XML file in there, eg. testXML.xml:


You should be able to use your XML parser using that as an inputstream:

Xml.parse(getResources().openRawResource(R.raw.testXML), Xml.Encoding.UTF_8, root.getContentHandler());

Try that.


I found a solution. Using Assets.
Here is the simple code example of how I did it.
I know I could have used XmlPullParser to simply load an xml file from res, but I wanted to use SAX parsing. This allows me to simply throw an XML string into the SAX parser for testing before I plug in the webservice.

It just uses a simple view with a Button to kick off the file load and a TextView to display the XML for now. I can get on with my parser 🙂

package com.martins.XmlParserTest
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

Button btn;
TextView tvXml;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    Button btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // Load XML for parsing.
            AssetManager assetManager = getAssets();
            InputStream inputStream = null;
            try {
                inputStream = assetManager.open("textxml.xml");
            } catch (IOException e) {
                Log.e("tag", e.getMessage());

            String s = readTextFile(inputStream);
            TextView tv = (TextView)findViewById(R.id.textView1);

private String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
    } catch (IOException e) {

    return outputStream.toString();


Raises exception due to incorrectly formed XML (line1,Pos0).

You tell parser that the encoding is UTF-8 and if it isn’t you may get various errors (depending on parsers). If you are using non-xml editor to edit your XML it may save the file in a different encoding regardless what you declared it to be in the XML document.