How to write to a specific line in a file - android

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.

Related

How do I pick selective html content in my webview in android?

I am currently trying to import selective headline from html content in my webview. I am looking at wide variety of options like json parsing or any hack will do. I was wondering if anyone has had experience with this or a brief idea on how to go about this?
Here's my example:
This is my html file content:
<div><h1><span class = "headline"> Some depressing title </span> <span class = "source" > ABCD </span> </h1> <br/> <span class = "body"> crappy body content which I do not need </span></div>
I just want to retrieve "headline" and "source" from this html in my webview, nothing else(not the body ). How do I go about defining a parameter to retrieve these? Any clues on how to do it?
Thanks!
Step 1: get the HTML source from your WebView - see this question. You basically create a JS interface that extracts your HTML source to a Java String.
Step 2: Use an HTML Parser (for example JSOUP) to parse the JAVA String into a format that you can handle easily.
Step 3: Use the parser to extract your relevant information. Here, you could use getElementsByTag('span') to get all your spans, then filter by class; or you could directly use getElementsByClass('healine') and getElementsByClass('source').
In general, you can retreive the HTML source and parse the DOM in all cases.
Edit: if you don't want to use a parser, you can extract your information by using searches on the HTML source string (finding the correct classes, then finding the indexes of '<' and '>' caracters to parse the information. This way is harder, less efficient, and less flexible, but it can be done.

Best way to dynamically display news articles (text, images)? Other than WebView?

I'm writing an Android app that reads RSS feeds, fetches HTML articles, processes the article's HTML to only store the important stuff (story body, including paragraphs and images/image captions, etc), and display it to the user.
I've done everything except for the final step.
The articles will obviously have varying text, varying image positions, etc. and I want to be able to preserve the order of those elements (as they were when fetched).
What is the best way to implement this? I don't really want to use a WebView...
Thanks in advance.
EDIT
Please see comments of accepted answer for my solution.
The best way I could see to do something like this would be to escape each of the html tags and handle each appropriately. Assuming your not interested interested in the head element and metadata you could do something like the pseudo code below for the following html page
<html>
...
<head>
...
</head>
<body>
<h1> some text probably your title </h1>
<p1> first paragraph </p1>
<p2> second paragraph </p2>
<img src='/some_url' title='some_title'>
</body>
</html>
Now for what you need to do also note that how the html page is actually set up will depend on webpage/rss feed so modifications may will probably be need to be made for many sites none the less you'll want to do something like this: Not when i say llok for I mean some how search for substring (java if on device) anything you wish off device
find("<body>") everything before can be thrown away
find ("<img" or "<p1" or "<h1" or "<div") handle accordingly
(more then likely this will change on source of page)
but for say <p1 found
find (">") represents end of tag attribute pull all of this until delimiter tag "</p1>"
there you've got your first paragraph
for image tag
ie. find("<img")
then find("title=") or find("src=")
the substring after these will be The image title and source file for the image respectively not that these values will be wrapped in one of ' or "
This isn't a complete solution but hey I have seen what you've tried so its a starting point

Adding a Resource Bundle Link to load a new Activity

I have a paragraph of text with a url at the end of it. I have the text and link in the strings.xml. Is there anyway to get it to load a new Activity from the strings.xml file? I'm assuming I'll have to break up the paragraph text and link, but thought I'd check.
strings.xml:
The quick brown fox can be found at: http://thequickbrownfox.com\n more text here
I need to change the hardcoded url "http://thequickbrownfox.com" to load a screen inside my app instead of a page on the web.
strings.xml is purely an abstraction mechanism used for string lookup to facilitate multi language support etc; you cannot use it to load activities or do anything else programatically. It sounds like you are actually talking about parsing the url out of a particular paragraph stored within strings.xml and then depending on what that url is, you invoke a corresponding activity.
If this is the case then you can either parse out the url from the paragraph and respond accordingly.
OR
you can store your paragraph as one item in strings.xml and your url as another item and combine them programmatically in your code.
Either approach can be fine depending on what you are doing.

extracting strings from KML file

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.

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.

Categories

Resources