如何在android的java中获取ElementById [英] How to getElementById in java for android

查看:155
本文介绍了如何在android的java中获取ElementById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b


  1. 阅读html文档到代码

  2. 使用getElementById通过id获取特定部分的代码,然后保存该字符串。
  3. 创建一个文本文档以将该字符串保存为

  4. 运行文本文档(将成为html文档)

这可能吗?

解决方案

你需要一些像Jsoup一样的html解析器。它简单而有效的库。在那里,你可以看到一个我的随机实现,我回答了另一个类似于这个问题的问题。 .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 {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
尝试{
Document doc = Jsoup.connect(URL).get();
Log.i(DOC,doc.toString()。toString());
元素elementsHtml = doc.getElementById(id); (Element element:elementsHtml)

{
Log.i(ELEMENTI,URLDecoder.decode(element.text(),HTTP.UTF_8));
}

} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();



$ b $ p
$ b

在这里你可以得到Jsoup



http://jsoup.org/



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



编写文件不要忘记在 Manifest


中添加连接互联网的许可

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?

解决方案

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

这篇关于如何在android的java中获取ElementById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆