Say I have a long list like the following:
العربية
مصرى
Asturianu
Azərbaycanca
Žemaitėška
......
How can I convert it to a string-array in xml format?
<item>العربية</item>
<item>مصرى</item>
<item>Asturianu</item>
I actually could not find anything with online search relating xml formats. but I finally came up with this solution.
I copy the list to excel and arrange the in 3 cells and copy-paste to xml. Then and cut paste again all to a new single cell in excel. Replace all " "(single space without "" created for no reason) with ""(just blank without "")
It works, I hope I have not taken the time of too many people
Related
What I have been trying to do, is to use a XML file to access a array of data that I wish to have stored on the web. The file looks like this:
<resources>
<60020> 13.5 </60020>
<50020> 11.2 </50020>
</resources>
The program grabs the user's GPS coordinates and then geocodes them to a zip code, that part I have down. What I wished to do after that, was to compare that zip code to the zip codes in the XML file and then return a integer based on the closest zip code. For instance, if the user has zip code 60013, then the program would see that 60020 is the closest zip code, and then return 13.5 based on that search.
I have already tried making a SAXParser, and I can return the first value, however, I cannot how to force the SAXParser to read through the whole document to find the nearest value. I also already have a algorithm to find the nearest value (simple for loop checking for difference in values), but again, I am stuck on the whole idea of how to check the whole list of values. Am I using the wrong type of storage method? Should I be using SQL? Or a different parser?
Thanks for your help.
I better idea would be to round off the zip code first and then search it in the XML file.
I would recommend Xpath Api for querying the XML file.
I very good tutorial about Xpath can be found here
http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
and as SAXparser itself will look for tags, it won't roundoff them, allover XPath is more prefered for searching XML documents, as it is relatively easy.
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!)
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!
I can't find if there is a way to write very long strings in XML in multiple lines without affecting string behavior in the code.
I have FAQ section and Answers to some questions are EG 3-4 sentences, some 400 characters long. So for each "Enter" ("Return") that I've pressed in my Strings file, I get new paragraph when displaying this in the app.
I've read here that concatenation is not working, but I do not really need it, I need to escape new paragraph that occurs when enter is pressed.
Also, you can use \n:
<string name="about_message">
Line1\nLine2\nLine3
</string>
Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.
I have some reference data in a text file (~5MB) that I want to use with might android application.
The file is of the format:
1|a|This is line 1a
1|b|This is line 1b
2|a|This is line 2a
2|b|This is line 2b
2|c|This is line 2c
What I want to know is the most efficient way (less memory, fast, size etc.) to use this file within my application.
a.) Should I save the file as a raw resource and open and read the whole file whenever I need a certain line.
b.) Should I convert the file to XML and use XPath to query the file when ever I need to look up a value
<!--sample XML -->
<data>
<line number="1">
<entry name="a">This is line 1 a</entry>
</line>
</data>
c.) Should I just copy & paste the whole file as a static string array in the application and use that.
... any other suggestions are welcome.
[EDIT]
I will also need to search this file and jump to arbitrary keywords e.g. "line 1a".
XML will always take longer to read than simple text or CSV files. What XML gives you in the tradeoff is a highly structured and reliable way of storing and retrieving data. XML files are, as you can see in the examples above, a good 2-3x larger than the data they actually contain.
If you're sure that you're never going to run into the "delimiter" character in your simple text file, then that would probably work just fine, purely from a file speed perspective.
You have not provided enough information to answer this question. However, if I were a betting man, the answer is probably "none of the above".
I will also need to search this file
What does this mean? You are searching by some string key? By some regular expression? By a SQL-style query string where certain portions of a line are interpreted as integers versus strings versus something else? By a Google search-style string?
Each of those answers probably dictates a different technology for storing this information.
I will also need to...jump to arbitrary lines.
Why? How are you determining which "arbitrary lines" you are "jump"ing to: key? line number? byte offset? search results? something else?
And, of course, there are other questions, like:
How often is this data updated?
How is this data updated: new version of the app? download the whole file? download deltas/diffs? something else?
Is the data ASCII? UTF-8? Something else?
and so on.
Something that size that must be searched upon suggests "use a SQLite database", but some of the other answers might steer away from that solution.
If you are talking about very small amounts of data, the Android XML compiler can produce very efficient binary representations for you that you can access just like XML. On the other hand if the data is very large at all, and you need arbitrary queries, I would expect SQLlite to win out on performance (as well as flexibility). A small benchmark should be easy to write and would give you a good idea as to the basic tradeoffs involved.
Flat-files would be a last option, imo, but could work if the file isn't very large.
If you define efficiency as (less memory, fast, size etc.), a flat or delimited file will be faster to load and save.
However, people use XML because they are willing to trade some of that speed for XML's greater flexibility and ease of use.