Simon Simon - 9 months ago 58
Java Question

Convert String to arraylist using split

Is it possible to convert below String content to an arraylist using split, so that you get something like in point A?

<a class="postlink" href=""><br/>
<a class="postlink" href=""><br/>
<a class="postlink" href=""><br/>

Point A (desired arraylist content):

I am now using below code but it doesn`t bring the desired output. (mirror for instance is being added multiple times etc).

Document doc = Jsoup.parse(string);
Elements links ="a[href]");
for (Element link : links) {
Node previousSibling = link.previousSibling();

while (!(previousSibling.nodeName().equals("u") || previousSibling.nodeName().equals("#text"))) {
previousSibling = previousSibling.previousSibling();

String identifier = previousSibling.toString();

if (identifier.contains("Mirror")) {

Answer Source

Fix your links first. As cricket_007 mentioned, having proper HTML would make this a lot easier.

String html = yourHtml.replaceAll("<br/></a>", "</a>"); // get rid of bad HTML
String[] lines = html.split("<br/>");

for (String str : Arrays.asList(lines)) {
    ... // you can go further here, check if it has a link or not to display your semi-colon;

Now that the errant <br> tags are out of the links, you can split the string on the <br> tags that remain and print out your html result. It's not pretty, but it should work.