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?

  • Replacing a character by another character in a string in android?
  • ImageView be a square with dynamic width?
  • Action Bar Icon hidden after SearchView expands
  • Android - Launching Google Navigation from an app
  • How to Detect Physical object in Android Augmented Reality?
  • Complete list of MediaPlayer error codes
  • Related posts:

    Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed) while bui...
    Android floating buttons over a view
    Android: download large file
    Android apps, communicating with a device plugged in the USB port
    Programmatically change the width of the Button in android
    Are Android broadcasts received in order?
  • Android WebView “tel:” links show web page not found
  • Android Dashboard Pattern
  • Android Notification Action is not fired (PendingIntent)
  • Android: Open file with intent chooser from URI obtained by Storage Access Framework
  • Permission to read data from SD Card
  • 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.