0

I'm designing an app which involves parsing a large XML(which include lots of text) and keeping the serialised data accessible throughout the application. I intend to have a data object which will keep the data stored and some classes can access the data. I alway need to show only part of the text.

yes my problem is very similar to this question maybe it's the same problem, but I don't want to use this solution, if is it possible.

I would like to know how to load the data, where to load it and how to keep it accessible throughout the application. It's impossible to load it everytime in onCreate() method, for example when the screen orientation is changed.

Best way for me, would be if I could load (parse) the xml file into one object and this object keeps accessible throughout the application, but I don't know where, how to load it and how to keep it accessible throughout the application. This object has everything what I need.

some possible solutions:

  • Singleton (this would load data into one object) use this or something else ?
  • Using a SQLite solution like there (same link as above) (I don't want use this)
  • Use lazy loading (load the part of text, which I actualy need) (I don't want use this as well)

Could someone say what would be best solution ? And describe how to do it ?

(Sorry for my english)

  • EDIT 1: xml structure:

    < root >
         < some elements >
         < /some elements >
    
         < element with lot of text >
             text.....
         < /element with lot of text >
    
         < element with lot of text >
             text.....
         < /element with lot of text >
              .
              .
              .
         < element with lot of text >
             text.....
         < /element with lot of text >
    < /root >
    

if it will be helpful. Text in one element usually includes 5000 - 15000 characters.

Community
  • 1
  • 1
Pauli
  • 538
  • 8
  • 22

3 Answers3

2

Singletons have various general disadvantages; I'd personally advise against using them for anything that is not stateless and independent of everything else (including files or the network, where you load your XML from).

The main problem in regards to your application is, however, that Android is allowed to start and stop your application at will. That means, that your singleton would need to get re-created often, e.g. you'd need to parse your data quite often (restarts do not preserve singletons!).

Without knowing anything about your XML, it is hard to recommend a specific approach; if applicable, do an initial parsing into a database, so that you can then easily receive individual values from the database without additional parsing.

dst
  • 3,307
  • 1
  • 20
  • 27
  • Good answer. I'm doinga project with a similar setup. My singleton holds a reference to my dataset, which initially checks a database. If not found, it will pull the .xml from the web, then populate the database. Then, because the singleton is not preserved, the next time around it will be able to pull from the database. – mt523 Aug 19 '14 at 20:44
  • If I understand to it. You download the xml file, then populate the database and then if you need the data you get it from database while app is running ? – Pauli Aug 19 '14 at 20:55
  • Yes, that is the basic concept. The inner workings of that approach depend on your actual application (when to update/populate your database, where to store it, how to access it in your application, etc). – dst Aug 20 '14 at 19:10
0

Extending Application is one solution. You can use this example.

Community
  • 1
  • 1
eldjon
  • 2,800
  • 2
  • 20
  • 23
0

I decided for the SQLite database. I read many discussions about the Singleton and some was negative (hard to test, some troubles with synchronization). So to avoid the problems I will implement parsing xml file into DB.

Pauli
  • 538
  • 8
  • 22