Need help with reading an external XML/JSON/TEXT file.

Hope everyone is doing fine.

Needed a little guidance and help around solving this problem. I want to read xml/json files from within my lua code instead of hardcoding some texts… Every tutorial or guide i found the teacher was basically just writing and creating a file from within lua code and then reading the same file that they created. Upon further investigation i managed to find out that created files end up in sandbox directories of corona simulator and cannot be accessed by developer due to some reason. 

I wanted to know if there is any way whereby a person can post an xml structured file or Json file somewhere in the root directory and have it read from within the lua code in visual studio…

Thank you for taking the time to read this.

Are you familiar with the differentCorona directories and paths?

*You Resource Directory is essentially a read-only directory so you should try working with the Documents Directory which will show up in the sandbox if you are working with the simulator.

You can access your sandboxed files.  In the Corona simulator go to File >> Show Project Sandbox to find the path.

You are running into a couple of basic programming issues.

Lua, like most languages can only open, read, write, append to and close flat files. They can be text/ASCII or binary. A couple of languages have JSON parsing built in, others come from installed libraries. Corona includes the JSON library, which you can simply require in any module where you plan to use it:

local json = require("json")

And you will get two simple functions:

local someJSONEncodedString = json.encode( someLuaTable ) 

returns a JSON encoded/serialized string and stores it in a variable. It’s twin, is the json.decode function:

local someLuaTable = json.decode( someJSONEncodedString )

returns a Lua table from the JSON string. Then you can write that encoded string to a Lua file. You can open, read, and then json.decode() a string you read from a Lua file and voila! you have a Lua table.

As far as accessing this, Corona’s history is around Mobile apps, so many of the file IO rules come from those enforced by mobile device rules. This means working within the app’s sandbox. Desktop apps are moving towards this model.

You can access the sandbox files as mentioned above. You can also include files in your system.ResourceDirectory (i.e. the folder with main.lua) since you’re developing apps there, but they will be read-only and there are additional restrictions on Android. But if you want to read/write to this file, you will have to detect the first time the app runs (file doesn’t exist in system.DocumentsDirectory for instance) and copy your starter file there inside your app.

We did a tutorial on copying an SQLite database from system.ResourcesDirector to system.DocumentsDirectory here:
https://coronalabs.com/blog/2015/05/19/tutorial-initializing-a-writable-sqlite-database-from-a-read-only-database/

It however will work for any file.

Lua tables and XML are not all that friendly. JSON is pretty much just key-value pairs. XML not only has key-values (tag, contents inside the tags), but it has attributes:

\<sometag someattribute="somevalue"\>some data\</sometag\>

This doesn’t map well to Lua tables. We have an unsupported XML.lua file that an engineer back in 2012 put together, but he has long since moved on. It works for most XML. You can get it here:
 

https://github.com/coronalabs-samples/business-app-sample

And there is an rss.lua module that uses the xml.lua module. It will likely work for you, but JSON is just so much nicer.

Finally, our network.request() API can be used to get/fetch JSON from web servers. This is another way to get content in your app.

Rob

Thank you sporkfin & Rob,

Although i did have placed my files in documents directory but for some reason but it did not work, i changed the installation folder of corona sdk to my c drive and then it was able to recognize and read the file.

@Rob

Thank you for your detailed answer, relying on JSON or XML for parsing was another thing that needed to be solved. So i figure according to your answer JSON is the better way of doing things due to support. 

Corona SDK picking up the .text/xml file from documents directory solved now after debugging and moving corona installation to C drive.
 

My target is basically to have some dialogue in a JSON formatted file and i want to read that file from my Lua code and read it in a controlled manner that i read only specific parts of it instead of hard coding dialogues in Lua.

Another Query

This needs to be done in a fashion that the game can be run without requiring an internet connection as well (Offline mode) . This will be a mobile deployment, will it be able to work without internet or would it require web server communication at all times. 

Thank you 

You can have .json files that’s part of your project. I did a trivia game several years ago, and I included a small subset of the questions in the system.ResourceDirectory, so if there wasn’t a network connection I had some content to work with, but with a network connects they got access to the full database of questions. I just had a PHP script that hit a MySQL database, fetched 20 random questions and returned a JSON object to a network.request() call. 

Rob