Hack-R Hack-R - 2 months ago 11
Java Question

How can I convert a Jsoup Document[] array to a String[]?

My question is the same as this one except that instead of a single

Document
I have an array (
Document[]
).

I normally use R, not Java, so I apologize if it should be apparent how to change the solution from the linked thread for the case of an array.

The solution for the case of a single
Document
object was:

String htmlString = doc.html();


My code to create the object was:

Document[] target = new Document[20];
for(int n=0; n < strvec.length;n++){
target[n] = Jsoup.connect(strvec[n]).get();
}


I tried a few things like creating the original
target
object as
String[]
, putting
.toString()
on the end of
Jsoup.connect(strvec[n]).get()
and elsewhere, but these attempts were unsucessful.

Answer

it is assumed that serves is an array of String containing the URL to connect, you do not need to create another array of Document

String[] result = new String[strvec.length];
    for(int n=0; n < strvec.length;n++)
      result[n]=Jsoup.connect(strvec[n]).get().html();