Skip to content Skip to sidebar Skip to footer

Html Structure Varying On Input

I have a string which contains dynamic HTML. The HTML can contain static image, maps, texts, links, etc. You can take a look at this link. The answer to this question is working w

Solution 1:

Reading your original question I see that you can achieve what you want this way:

  1. You have an anchor (a.favorite)
  2. You have to pick his grandchild (font in this particular case, but it could be an img or whatever)
  3. You delete the children of the original anchor
  4. and then you append the grandchildren as a new child!.

This may sound complicated but it is very easy, here you are a code example:

String html ="<a class=\"favourite\" href=\"LixWQfueLU\"><a href=\"LixWQfueLU\"><font color=\"#009a49\">Rohit Lalwani</font></a></a>";
    Document doc =Jsoup.parse(html);
    //The original anchorElement afav = doc.select(".favourite").first();
    //The grandchildElement select = doc.select("font").first();
    afav.remove();
    afav.appendChild(select);
    System.out.println(afav);

Output:

<aclass="favourite"href="LixWQfueLU"><fontcolor="#009a49">Rohit Lalwani</font></a>

Hope it helps!

Post a Comment for "Html Structure Varying On Input"