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
How to getElementById in java for android
提问by Darcy
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 string to
- Run the text document (going to be a html doc)
- 将 html 文档读入代码
- 使用getElementById通过id获取某部分代码,然后保存这个字符串。
- 创建一个文本文档以将此字符串保存到
- 运行文本文档(将成为 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
For writing the file use FileWriter
or 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