Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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

like image 782
marcoqf73 Avatar asked May 11 '12 11:05

marcoqf73


People also ask

Which XML parser is recommended for Android?

We recommend XmlPullParser , which is an efficient and maintainable way to parse XML on Android. Historically Android has had two implementations of this interface: KXmlParser via XmlPullParserFactory.

What is Android assets folder?

Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data.

What is XML parser Android?

Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document.


1 Answers

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

InputStream is = getAssets().open("xmlfile.xml")
like image 185
Rawkode Avatar answered Oct 07 '22 00:10

Rawkode