简体   繁体   中英

Get text after after html tag using Jsoup and create ArrayList

I am trying to get text from the HTML document into ArrayList using Jsoup. My document looks like this.

<p>
   <b>1</b>First Text
   <b>2</b>Second Text
   <b>3</b>Third Text
   .
   .
   .
   .
</p>

From the above document, I am want to create Array like below.

list{[First Text],[Second Text],[Third Text]}

Appreciate your help.

Tried with below code, but getting numbers only.

Document doc1 = Jsoup.parse(chapter);
Element vve = doc1.body();
Elements vvv = doc1.select("p").select("b");

My Result is

vvv.get(0) = <b>1</b>
vvv.get(1) = <b>2</b>
.
.
.

Here is the complete code which I completed last night.

        Elements elements = doc.body().select("p").select("b");
        for(int i=0; i<elements.size(); i++){
            Element para = elements.get(i);
            versesList.add(para.nextSibling().toString());
        }

This will help

Elements elements = doc.body().select("p");

for(int j=0;j<elements.size();j++){
     Element para = tblelements.get(j);
     String value = para.select("b").get(i).text();
}

Lets try this:

final String html = "<p> ... "; // your HTML here

Document doc = Jsoup.parse(html);
List<String> list = new ArrayList<>();


for( Element element : doc.select("b") )
{
    list.add(element.nextSibling().toString());
}

Note: You can use List<Node> as well, then you only have to remove the toString() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM