How to save data structures in android?

I want to save some data from such structures:

class Project { ChildA a; ChildB b; } class ChildA { ChildC c; ... } ... 

I created data such as:

 Projet x = new Project(); xa = new ChildA(); xac = new ChildC(); 

... and I want to save it to an external file on the SD card of my device so that other applications can read it (and users can open / copy it).

I thought I should use the DOM parser because my data structures are not very large, but I do not find tutorials or anything like that. Is there a better way to save this information?

If not, are there instructions for using parsers in android?

+4
source share
1 answer

If you want your data to be available for other Android applications, it is recommended to use the Content Provider . If, on the other hand, you want your data to be used in applications other than android, you need to "export" it. You need to do this yourself.

Now I can present three options:

  • Creating XML Files
    Unfortunately, Android does not include everything that is needed to conveniently create XML files. In particular, javax.xml.transform not available, and adding platform classes is not recommended. This means that you have to write this transform class yourself
  • Create JSON Files
    JSON is well supported by the Android API. See here and here.
  • Creating Java Serialized Files
    As long as your objects implement the Serializable interface, then they can easily be written to files. I never had to do this, but it should be and examples should be available online. (For example, Google came up with this mini tutorial )

When you store files privately, consider using openFileInput and openFileOutput . If you want to explicitly store on the SD card, you must use getExternalStorageDirectory () to retrieve the root folder.

My personal recommendation would be to use JSON files. This is a simple format that works well for most cases and is widely available. Also, de / serialization is dead.

+13
source

All Articles