0

I am running into the following issue when trying to parse in an XML file into my Kotlin Application:

java.io.FileNotFoundException: /src/main/res/locations.xml: open failed: ENOENT (No such file or directory)

Below is the code responsible for handling the file loading:

fun parseToObject() {
    val thread = Thread(Runnable {
        try {
            val xml = File("src/main/res/locations.xml")
            val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml)
            println("Root Node: " + doc.documentElement.nodeName)
        } catch (e: Exception) {
            print(e.message)
        }
    })
    thread.start()
}

Anyone know what I might be doing wrong? I've tried using full paths as well as shorter ones and it just seems to not like any of them.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
Quasimodo
  • 103
  • 1
  • 8
  • instead of specifying relative path try to give it complete path – mightyWOZ Dec 19 '19 at 13:38
  • @mightyWOZ I have done :/ still get the same issue. I'm pretty new to Kotlin, is there a permission I have to give to allow it to parse files or something? – Quasimodo Dec 19 '19 at 14:09
  • the above or try accessing the resources folder with some of: https://stackoverflow.com/questions/42739807/how-to-read-a-text-file-from-resources-in-kotlin Using the `File` approach might cause an errors because the program might be running in different location or not having bundled resource folder, the linked question have plenty of different solutions, I'd start with `this.javaClass::class.java.getResource` or the accepted and topvoted one. – itwasntme Dec 19 '19 at 14:10
  • @itwasntme thank you for linking that, I will have a go using that method instead – Quasimodo Dec 19 '19 at 14:18
  • It's weird, now using that method I get a null URI error. It definitely exists though, its a 14.1 Mb file and opens normally in Android Studio. – Quasimodo Dec 19 '19 at 14:25
  • Which approach have you used, and what parameter do you provide to it? When accessing the resources you no longer need the `src/main/res` prefix. Could you post the exception you get. And did you tried other approaches? – itwasntme Dec 19 '19 at 14:36

1 Answers1

0

You can't access project files as Files at run time, only as InputStreams. And you can't necessarily access specific files in res unless they are in res/raw/. Otherwise, they may not even exist in the installed app due to optimizations.

You can put the file in main/res/raw/ or in main/assets/. You can get the file contents as an InputStream (not directly as a File).

Using a raw resource:

val doc = Resources.openRawResource(R.raw.locations).use { xmlInputStream ->
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlInputStream)
}
println("Root Node: " + doc.documentElement.nodeName)

Using an asset:

val doc = context.assets.open("locations.xml").use { xmlInputStream ->
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlInputStream)
}
println("Root Node: " + doc.documentElement.nodeName)

Pros and cons of raw resources vs assets:

Raw resources have IDs and can be referenced by other resource XML files. Assets can only be accessed via String file-name look-up, but they can be organized into subdirectories within assets.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154