How to getElementById in java for android

若如初见. 提交于 2019-12-12 02:44:44

问题


Here is what I'd like to do.

  1. Read an html document into code
  2. Use getElementById to get a certain portion of code by id, then save this string.
  3. Create a text document to save this string to
  4. Run the text document (going to be a html doc)

Is this possible?


回答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;

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



来源:https://stackoverflow.com/questions/6810838/how-to-getelementbyid-in-java-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!