Skip to content Skip to sidebar Skip to footer

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 this

Solution 1:

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;

publicclassstartextendsActivity {

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       try {
        Documentdoc= Jsoup.connect(URL).get();
        Log.i("DOC", doc.toString().toString());
    ElementselementsHtml= 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

Post a Comment for "How To Getelementbyid In Java For Android"