2

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;

So anyone got an answer? thanks. (Btw, i use Jsoup)

1
  • 2
    "that did not work"... In what way? Did you get compilation error, runtime exception, results did not meet your expectations? Commented May 25, 2011 at 18:26

1 Answer 1

1

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?

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

I now changed my code to ----Elements values = defaultDoc.getElementsByTag("option"); s[i++] = el.attr("value");---- I thought that it was "value" instead of "values", since <option value="VALUEIWANT"> says "value" (wich I wanted to get). Correct me if i'm wrong pls.
Then it's value indeed, however in your question you talk about values. However does your code work now? It should or other things may go wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.