2

I want to make something that searches through an XML file, to see if a string, entered by the user, exists. My XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="dict_nl_array" ><item>ADHD</item><item>ADSL</item><item>AMvB</item><item>AOV</item><item>AOW</item><item>uniteit</item></string-array></resources>

The only difference is that my XML is about 500 times as long as this. I tried loading the arraylist (xml) directly, but then I get this error:

Failed adding to JNI local ref table (has 512 entries)

Which seems logical, because its a gigantic list of items. So, I was thinking, can I search through that XML file, without having to load it completely?

Ofcourse, other suggestions to do this are welcome! (If possible, with an example, I'd greatly appreciate that!)

laarsk
  • 862
  • 6
  • 17
  • 36
  • I've answered a similar question here http://stackoverflow.com/questions/12500947/how-to-search-for-words-in-string-xml You should look at using a database. – user Oct 10 '12 at 14:56
  • Ok, sounds reasonable, but is there a way to easily convert an XML to a SQLite database? – laarsk Oct 10 '12 at 15:03
  • Your xml layout will be difficult to parse into a database at runtime but you could put the words directly in to a pre-built database and bundle it with your app in the assets folder. You'll then simply build a database on the device from than bundled database. – user Oct 10 '12 at 15:06
  • Or you could put the words in to an ordinary xml file and put it in the `xml` folder in the `res` folder. You could then use one of the existing xml parsers and insert the values in to a database. But I would just build a database and ship it with the app itself. – user Oct 10 '12 at 15:09
  • Do you have tips on how to do that? I've never done anything like that before :P I do know how to work with MySQL though... – laarsk Oct 10 '12 at 15:10
  • I don't know what tips to give you, what I can give you is a link to more info on the topic http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database . In there you can also find a github library project that you could use so you don't have to write the database copy implementation yourself. There are also many tutorials out there, it's impossible to not make it work. – user Oct 10 '12 at 15:15
  • Thanks! I'm using the SQLite Database Browser (found via Google, hehe) to import a CSV of my XML data, to create a SQL file. Now I just gotta find out how to use SQL files, and how to use databases in Android... – laarsk Oct 10 '12 at 15:33

1 Answers1

1

Late answer, but it might help anyone else with this problem!

Based on this question, you can avoid this error by encoding the resource as a string, rather than a string-array.

<string name="array">ADHD#ADSL#AOV#AOW</string>

The # character separates the entries. To speed up the reformatting, just Find/Replace </item><item> with #. Then you can parse the string into an array like so:

myStringArray = getResources().getString(R.id.array).split("#");

Hope this helps!

Community
  • 1
  • 1
Eoin
  • 833
  • 2
  • 13
  • 24