Using XML to store data

吃可爱长大的小学妹 提交于 2019-12-11 02:41:51

问题


I am looking to use an XML file to store the data my Android app generates. With that in mind, I have two questions for the community:

  1. Is XML the best way to store data on Android and most efficient in cases where data may be added or altered every second or less then a second.

  2. If XML is indeed the best for the scenario described in #1, how do I go about setting it up?


回答1:


1.) Is XML the best way to database data on android and most efficient in cases where data may be added or altered every second or less then a second.

Definitely not.

2.) If XML is indeed the best for the scenario described in #1, how do I go about setting it up?

If you plan to store data just locally, the best way would be SQLite which works as a local database on every device.

If you later plan to synchronize this data with a central database, you may do this asynchronously within an AsyncTask or a Thread which would run periodically, but writing each second into a XML file is a bad idea as far as performance goes.

It's probably also a bad idea synchronizing a remote database at each insert/modification/deletion operation as if you had many users you could collapse the remote database.

I think the best approach is (as previously said) having a local database where you would store that data, and implement a webservice in the remote side if needed and use it to periodically synchronize both databases.




回答2:


I would use JSON over XML and I would highly consider using GSON from Google. You maybe want to consider writing directly to a database with it's own structure and use transactions and sets. Is there are reason you want to go through JSON/XML?




回答3:


XML is one of the worst ideas to keep local data in Android.

Most common used is SQLite available on the Android platform, but it all depends on what data and how you want to use.

In many mobile applications you don't need the relational database for one of the following reasons:

  • You have no relational data (i.e. settings) => no point in making relational tables with 1 record each
  • You have small, and dynamically changed data (like cache for downloaded content)
  • You don't need to search for data (using indexes etc.)

What alternatives can be used? Shared preferences - simple key/value storage of primitive objects Data serialization - for your consideration - binary (native java), JSON, parcelable (can be combined with the shared preferences)

For most of my app I'm currently using the binary serialization for "local storage". - It's fast enough (usually much faster than starting the local SQLite engine) - It's extremely easy and quick to implement, especially when you are using it for json/xml downloaded data parsed to POJO objects. All you need to do is just put "extends serializable" and put few lines of code to serialize/deserialize whole structure - You can use those same classes for keeping data locally and communication with backend

Of course - it all depends from the situation - if you want to keep locally log of data from some sensor, or allow others apps to use this data, have to quick filter 1k+ records, or you really like to write few hundreds lines of code SQLite will be the best option for you. But most of mobile applications has no clear reason to use the relational (and trust me - not perfect one) engine.



来源:https://stackoverflow.com/questions/22021968/using-xml-to-store-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!