extracting strings from KML file - android

I am extracting strings from KML file, if the string contains special character like !, #, #, ', " etc. its using codes like '
I am not able to extract entire string if it is like above, by calling getNodeValue(). It is terminating the string at special character.
<name>Continue onto Royal's Market</name>
If i extract the string i am getting only ""Continue onto Royal". I want entire string as
Continue onto Royal's Market.
How to achieve this ?? If anybody familiar with this please reply to this one.
Thanks

Your problem has nothing to do with KML but is general for XML parsning:
Don't use getNodeValue(), as there is no guarantee in DOM that text isn't actually split over several nodes.
Try using getTextContent() instead.
You might also have to replace entities, as in: node.getTextContent().replaceAll("'","'");
In general I wouldnt use DOM at all for extracting data.
I'd use the XmlPullParser as its simpler to work with - and parses faster.

Related

Find Closest Value in an XML file within a Android App

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.

How to write to a specific line in a file

I have a HTML file and when a user clicks save, a date needs to be added to this html file.
I'm using FileWriter writer = new FileWriter(myFile, true); so it is appended to the end but I have </body></html> so the date is appended to the end of this which I don't want. The date should be appended before those tags. Is there a way to append to a specific place or a more efficient way to solve this problem?
You consider redesigning your code in a way that leaved the layout (HTML) appart from the data (the dates).
You could have a HTML containing the fixed string {{dates_here}} and store the dates in a database or separate file - and then combine them when needed for viewing by constructing an insertable piece of HTML and inserting it with a simple string replace.
With JavaScript, this is not possible, however, I have a solution. Just leave out the <body> and <html> tags, and simply append the tags after appending what's needed. Ever so long ago, it's possible to actually omit those two tags. Try it!
Well, you can keep a DOM in memory of HTML document.
Use JSoup library to make one from a file or String. It will also help you to alter structure in an object oriented way and save it back easily.

Performance at working with strings android

Could somebody tell me what is better in terms of performance?
Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'
Or should I save only the first one and concatenate ':' when needed at Java coding ???
Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...
Dog // English
Chien // French
Hund // German
Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.
If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.
Both from storage perspective and performance you should save only "abc";
getting extra data from disk takes far longer as some quick in-memory actions.
storing the same data twice is bad practice in general
If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
It's much faster then using '+' or '.concat()'

Keep Strings into .txt file or put them into a Database?

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.

Android: Is it more efficient to use a text file or an XML file to store static data

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.

Categories

Resources