Skip to content Skip to sidebar Skip to footer

Android Parse Html With Jsoup

I have a String html like that: > [CDATA[
index1:
class='ExternalClass4C236C53DAF34307B4580A8DA27FA373'>

>

Solution 1:

I don't know a simple solution for this. You can select the contents of the div elemets. But you get some index value this way you dont want.

One hardcoded soulution is like this:

public void testParseValue() {
    String html ="[CDATA[<div><b>index1:</b> <div" +
        "> class=\"ExternalClass4C236C53DAF34307B4580A8DA27FA373\"><p></p>" +
        "> <div>value1</div><div></div><div>value2</div>" +
        "> <div></div><div>value3 </div><div></div><div>value4" +
        "> </div><div></div><div>value5</div></div></div>" +
        "> <div><b>index2:</b> value6</div><div><b>index3:</b> value8</div>" +
        "> <div><b>index4:</b> valu7</div><div><b>index5:</b>value10</div>";

    Document doc = Jsoup.parse(html);

    Elements divs= doc.select("div");

    System.out.println(divs.get(2).text());
    System.out.println(divs.get(4).text());
    System.out.println(divs.get(6).text());
    System.out.println(divs.get(8).text());
    System.out.println(divs.get(10).text());
    System.out.println(divs.get(11).text().substring("index2: ".length()));
    System.out.println(divs.get(12).text().substring("index3: ".length()));
    System.out.println(divs.get(13).text().substring("index4: ".length()));
    System.out.println(divs.get(14).text().substring("index5:".length()));
}

It is written in Java so you may want to change the System.ou.println call to log call. You may also use the position of ":" character instead of the whole indexX: strings.

Post a Comment for "Android Parse Html With Jsoup"