问题描述
我如何得到一段HTML(值=valueIWant)的值,并让他们在一个数组?
我尝试以下,但没有工作:
How do I get the values in a piece of Html (values="valueIWant"), and have them in an Array ?I tried the following, but that didn't work:
HttpEntity entity5 = response5.getEntity();
String defaultString = EntityUtils.toString(entity5);
Document defaultDoc = Jsoup.parse(defaultString);
Elements values = defaultDoc.getElementsByAttribute("value"); //DropDownList Values
String s[] = {""};
for(int a=0; a<values.size(); a++){
s[a] = values.get(a).toString();
}
return s;
因此,任何人有答案吗?谢谢。 (顺便说一句,我用Jsoup)
So anyone got an answer? thanks. (Btw, i use Jsoup)
推荐答案
首先:是你的HTML正确解析?你能否提供 defaultString
的内容是什么?为 defaultDoc
有效期是否与文件编码有问题吧?
First of all: is your HTML parsed correctly? Can you provide the contents of defaultString
? Is defaultDoc
valid is there a problem with file encodings perhaps?
假设 getElementsByAttribute
实际返回的某些对象 - 注意,你有一个错字,值
而不是值
- 你当前正在填充与所有元素
-objects,而不是属性值的描述的数组。尝试类似如下:
Assuming getElementsByAttribute
actually returns some objects —note that you have a typo, value
instead of values
— you're currently populating the array with the descriptions of all Element
-objects, not the values of the attribute. Try something like the following:
int i = 0;
String s[] = new String[values.size()];
for(Element el : values){
s[i++] = el.attr("values");
}
这篇关于我如何从jSoup元素的数组? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!