java 如何在java for android中getElementById

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6810838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:21:50  来源:igfitidea点击:

How to getElementById in java for android

javaandroidhtmlgetelementbyid

提问by Darcy

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)
  1. 将 html 文档读入代码
  2. 使用getElementById通过id获取某部分代码,然后保存这个字符串。
  3. 创建一个文本文档以将此字符串保存到
  4. 运行文本文档(将成为 html 文档)

Is this possible?

这可能吗?

回答by Nikola Despotoski

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...

你需要一些 html 解析器,比如 Jsoup。其简单而有效的库。在那里你可以看到一个我的随机实现,我是为了回答另一个与这个问题类似的问题而做的。.getElementById(id);是您正在寻找的方法...

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

在这里你可以得到 Jsoup

http://jsoup.org/

http://jsoup.org/

For writing the file use FileWriteror any other class that allows you to write to file

用于编写文件使用FileWriter或允许您写入文件的任何其他类

Do not forget to add permision for connecting to internet in your Manifest

不要忘记在您的计算机中添加连接到互联网的权限 Manifest