marcoqf73
marcoqf73

Reputation: 1344

Read xml file from Android Asset folder

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:

I can't not Read from Assets folder:


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

Upvotes: 8

Views: 25053

Answers (6)

D.Roters
D.Roters

Reputation: 231

If you only want to get the value of a specific item inside the .xml you could use this:

object AssetUtils {

    fun readXmlItemByName(
        context: Context,
        assetFileName: String,
        itemName: String
    ): String? {
        try {
            // Open file stream from project asset folder
            val fileInputStream = context.assets.open(assetFileName)

            // Create buffer to stream file into
            val size = fileInputStream.available()
            val buffer = ByteArray(size)

            // Read stream into buffer
            fileInputStream.read(buffer)
            fileInputStream.close()

            // Write buffer to string
            val utf8: Charset = Charset.forName("UTF-8")
            val string = String(buffer, utf8)

            // Check lines for xml item name
            val xmlItemName = "\"" + itemName + "\""
            val lines = string.lines()
            lines.forEach {
                // Found parameterName in line
                if (it.contains(xmlItemName)) {
                    // Extract value
                    val value = it.substringAfter(">").substringBefore("</")

                    // Return value
                    if (value.isNotBlank()) {
                        return value
                    }
                }
            }

            return null
        } catch (e: Exception) {
            Log.e("AssetsUtils", "readXmlParameter(): ${e.message}")
            return null
        }
    }

}

Upvotes: 0

Bink
Bink

Reputation: 2201

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.

Upvotes: 1

Ajay Pandya
Ajay Pandya

Reputation: 2457

    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();
    }

Upvotes: 0

Anuj Sharma
Anuj Sharma

Reputation: 47

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.

Upvotes: 0

Mufazzal
Mufazzal

Reputation: 81

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;
}

Upvotes: 7

Rawkode
Rawkode

Reputation: 22592

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

InputStream is = getAssets().open("xmlfile.xml")

Upvotes: 12

Related Questions