External data in Android - android

I have hardcoded data in my Android application. This will be in tree format. like this
<makes>
<make>
<name>abc</name>
<id>1</id>
</make>
... 10 more entries.
</makes>
Since this data will be hard coded, in my application, what can i use to easily parse in in my application. I can simply load it from my assets directory, and use xml parsing, but is there any inbuilt ways to use it. I know i can make a String array from xml resource, but how can i use this structure.

You could easily store that table as an SQLite database. This link might be helpful, particularly step 6.

You can use the XmlResourceParser class, that implements the XmlPullParser interface, and will allow you to parse the XML file, loading it from the resources folder. After that, I would recommend defining a Make class with the two properties defined in the XML and populate a List or other type of data structure with objects of that class while you loop through the xml file.

Related

Add XML Serialized object to APK so it is copied into the app's private files directory

I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);

Advice on what I should be in making an application that reads multiple .txt files

This is not a coding problem question, I just need general advice.
I am making an application that populates a listfragment with multiple text files. What I want is for the user to select the intended article they wish to read and said article will be provided for them. I have already made a master/detail esk application but it is only reading in a static array for the articles (which will later be changed to an expanding list and more dynamic method for reading in the files)
My question is this: do I make a database for the ~30-40 text files and create an onlistclick--database adapter function or should I use Assetmanager (or something else).
I am new to android programming and this is my first proper application so any suggestions that would be suitable for a novice would be much obliged.
If those text files are going to be modified very often then yes, it is a good idea to create a local database to keep them into.
On the other hand if you only need those 30-40 fixed text files and you are planning to add more just through app updates then you can simplify your life by using the assets folder. Cycle through all the files in there and use them to create the list.
Code snippet for Assets usage:
AssetManager assets = context.getAssets(); //use 'this' if you are inside an activity or 'getActivity()' if you are inside a fragment
String files[] = assets.list("folder_inside_assets"); //use assets.listFiles("folder"); if you want an array of File objects

Android Xml resource parser node parsing only

I am new to Android development and I am wanting to parse a node only xml file using XML Resource Parser in android. I can only get it to parse an XML that contains attributes however I dont want my xml to contain attributes and I just want to pull out data contained in nodes only. Can I do this with XRP? There seem to only be methods for XML's containing attributes i.e. getAttributeValue for example
My xml looks like this:
<Streets>
<street>ABBEY WAY</street>
<district>WILLESBOROUGH</district>
<collectionday>FRIDAY</collectionday>
<jan>6,20</jan>
<feb>3,17</feb>
<mar>2,16,30</mar>
<apr>13,27</apr>
<may>11,25</may>
<jun>8,22</jun>
<jul>6,20</jul>
<aug>3,17,31</aug>
<sep>14,28</sep>
<oct>12,26</oct>
<nov>9,23</nov>
<dec>TBA</dec>
<street>ABBOTT WAY</street>
<district>TENTERDEN</district>
<collectionday>TUESDAY</collectionday>
<jan>10,24</jan>
<feb>7,21</feb>
<mar>6,20</mar>
<apr>3,17</apr>
<may>1,15,29</may>
<jun>12,26</jun>
<jul>10,24</jul>
<aug>7,21</aug>
<sep>4,18</sep>
<oct>2,16,30</oct>
<nov>13,27</nov>
<dec>TBA</dec>
<street>ACKERLEY COURT</street>
<district>STANHOPE</district>
<collectionday>THURSDAY</collectionday>
<jan>12,26</jan>
<feb>9,23</feb>
<mar>8,22</mar>
<apr>5,19</apr>
<may>3,17,31</may>
<jun>14,28</jun>
<jul>12,26</jul>
<aug>9,23</aug>
<sep>6,20</sep>
<oct>4,18</oct>
<nov>1,15,29</nov>
<dec>TBA</dec>
</Streets>
I want to create an array from the street node initially
Many thanks
You could also use Simple for Android. It's very easy and well documented:
http://simple.sourceforge.net/
IMHO a parser where I can get objects directly from a xml document is pretty neat :)

Where do I put (and how do I read) my own custom XML in Android?

Sorry, this seems like such an obvious question, but I just can't find an answer in the docs...
I have my own XML doc (not Android related) that I need to pull and parse at run time.
Where do I store it and what's the best way to retrieve it? Do I just read it as a text document into an XML parser? Or does Android come prepared to hand me back a parsed XML object from a well formed xml?
TIA
Put it in a xml folder in res.
You can put it in assets folder of your project.
For parsing you can use openXmlResourceParser(filename) method of AssetsManager class
XmlResourceParser parser = getResources().getAssets().openXmlResourceParser (String fileName);
Best way is to put your xml in assets folder and read xml from assets.
Read xml from assets

How to save parsed data in android?

I am creating an android application in which I am parsing so much XML data (XML data comes from a server) which contains Strings and image url's also.I need to use this data in many part of application. So for saving all these data I have used ArrayList and HashMap.I have declared ArrayList's and HashMap's variables in a single class as public static variables so I can access this data through a single class whenever I need.
And for images I have created ArrayList and placed in same single class same as other data(public static).Once I download a image through image url's I save those image drawables to these ArrayList variables so whenever I need any image again so I use these variables to get it.
Now my doubt is whether this approach is right or not. Please suggest me the right way.
Thank you
You should actually take a look at: http://developer.android.com/guide/topics/data/data-storage.html
It all depends on how you use the data, does it need to be updated all the time when the user opens the app you could take a look at this answer: How to declare global variables in Android? where you declare a global application and have the variables there.
Otherwise I would actually advice to use an sqlite database. http://developer.android.com/guide/topics/data/data-storage.html#db One good example is here: http://www.vogella.de/articles/AndroidSQLite/article.html

Categories

Resources