How to read XML file in android

I want to read xml file which looks like the following …
I have stored it in assets folder with

<ImageList SpriteSheetName="hh_gmw01">
   <Image Name="gmw01"     x="0" y="0" width="1047" height="752"/>
   <Image Name="hht1l01"   x="388" y="269" width="34" height="36"/>
   <Image Name="hht1l02"   x="147" y="99" width="85" height="33"/>

How do I get these values?

  • Failed to construct Notification: Illegal constructor
  • Debug Android library project with java source code
  • How to select the first item in a navigation drawer and open a fragment on application start
  • Using Camera in the Android emulator
  • Integrate my app with Contact
  • Android : Consequences of having targetSDK > BuildTarget
  • Related posts:

    Launching activity from widget
    How to update App Widget with list view from an Activity
    Android Studio Slow Debugging on Device
    Gradle and proguard: could not find method runProguard() for arguments
    How to add click action for the ImageSpan
    Wifi position triangulation
  • Android Live Wallpapers with OpenGL ES 2.0?
  • TextView in the center of the screen
  • Android: how to change the color of the datepicker divider?
  • Android XML: RuntimeException: Failed to resolve attribute at index 6
  • Android Wear Notification is not displayed if FLAG_NO_CLEAR is used
  • How do I get Google refund orders ?
  • 5 Solutions collect form web for “How to read XML file in android”

    Try this out first:

    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(;
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Load XML for parsing.
                AssetManager assetManager = getAssets();
                InputStream inputStream = null;
                try {
                    inputStream ="textxml.xml");
                } catch (IOException e) {
                    Log.e("tag", e.getMessage());
                String s = readTextFile(inputStream);
                TextView tv = (TextView)findViewById(;
    private String readTextFile(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = != -1) {
                outputStream.write(buf, 0, len);
        } catch (IOException e) {
        return outputStream.toString();

    There is several ways to read a XML in Android. My first option is DocumentBuilder since do not create an API version restriction (is available since API Level 1).

    An example from one of my projects:

    public Document parseXML(InputSource source) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            return db.parse(source);
        } catch (Exception e) {
            return null;

    How do you read the file and access the document values after this, well, its pretty basic, just Google it.

    To start with use DOM parser. Since its much lesser code, and easy to follow. SAX parser is just too much code to start with. People will argue that SAX is faster, yes it is, but DOM is easier, lesser code and lesser bugs.

    If you must move to SAX, first measure your response times when using DOM, and only if parsing is causing you the most pain, then move to SAX. Or else DOM does a wonderful job.

    Personally i wouldn’t recommend the DOM parser, try this instead, simple annotations can help you by using the Simple xml parser

    Try this for Xamarin.Android (Cross Platform)

    you need to store xml file into Assets folder with build action “AndroidAsset”.

    using System;
    using System.Xml;
    using System.IO;

    Code snippet to read xml file

    XmlDocument xDoc = new XmlDocument();
    Stream xmlStream = Assets.Open("textxml.xml");

    XmlDocument class implements the core XML Document Object Model (DOM) parser for the .NET Framework.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.