How to getElementById in java for android
Here is what I'd like to do.
- Read an html document into code
- Use getElementById to get a certain portion of code by id, then save this string.
- Create a text document to save th开发者_开发问答is string to
- Run the text document (going to be a html doc)
Is this possible?
You need some html parser like it is Jsoup. Its easy simple and effective library. Down there you can see one my random implementation, I did for answering another question similar to this one. .getElementById(id);
is the method you are looking for...
import org.apache.http.protocol.HTTP;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class start extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Document doc = Jsoup.connect(URL).get();
Log.i("DOC", doc.toString().toString());
Elements elementsHtml = doc.getElementById(id);
for(Element element: elementsHtml)
{
Log.i("ELEMENTI",URLDecoder.decode(element.text(), HTTP.UTF_8));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here you can get the Jsoup
http://jsoup.org/
For writing the file use FileWriter
or any other class that allows you to write to file
Do not forget to add permision for connecting to internet in your Manifest
精彩评论