8

I'm trying to read the same file "xmlfile.xml" from the assets folder and also another copy from the SD card sdcard/download/.

I can Read from SD Card:

  • unfile Return True
  • Esite Return True

I can't not Read from Assets folder:

  • unfile Return False
  • Esite Return False

This code il NOT Working

        File source = new File("file:///android_asset/xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This code il Working

        File source = new File("/sdcard/" + "download" + "/" + "xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

someone can explain me how can I read the file from the Assets folder.

thanks marco

marcoqf73
  • 1,306
  • 3
  • 14
  • 18
  • see ans given by me at http://stackoverflow.com/questions/1372470/parse-local-xml-file-in-android/8413361#8413361 – Khan May 11 '12 at 11:53

5 Answers5

12

To open an asset you'd need the following piece of code:

InputStream is = getAssets().open("xmlfile.xml")
Rawkode
  • 21,990
  • 5
  • 38
  • 45
7

Use this function

// Converting given XML file name to String form
String mOutputText = getxml("yourxml.xml");

/**
 * Function used to fetch an XML file from assets folder
 * @param fileName - XML file name to convert it to String
 * @return - return XML in String form
 */
private String getXml(String fileName) {
    String xmlString = null;
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(fileName);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        xmlString = new String(data);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return xmlString;
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Mufazzal
  • 81
  • 2
0
    try 
    {
        AssetManager aManager = Class.getAssets();
        InputStream iStream = aManager.open("file.xml");
        int length = iStream.available();
        byte[] data = new byte[length];
        iStream.read(data);
        assetString = new String(data).toString();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
Bunny
  • 1,044
  • 12
  • 23
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
0

This will surely help you. For reading a file form assets folder you need a InputStream Object.

Syntax:

InputStream yourobj=getApplicationContext().getAssets().open("Path to xml file");

So the live code can be:

InputStream in_s = getApplicationContext().getAssets().open("www/application/app/client/controllers/data1.xml");

Here data1.xml is the file inside the path.

techspider
  • 3,370
  • 13
  • 37
  • 61
0

I found it easier to do the following:

val xmlStream = context.resources.getXml(R.xml.example)

I believe this is a subset of XmlPullParser, but it has worked well enough.

Bink
  • 1,934
  • 1
  • 25
  • 41