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

Questions:

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.

Thanks

How to&Answers:

Create a raw folder under res

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

/res/raw/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.

Answer:

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. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    btn.setOnClickListener(new OnClickListener() {

        @Override
        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);
            tv.setText(s);
        }
    });
}


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);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
}
}

Answer:

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.