Search code examples
javajsoupjava-11

Using JSOUP How to Extract all elements from one tag & add in another tag?


I need to set <a> tag to all <h2> tags in the document. Document contains few <h2> tags with <a> tags & few are without <a> tag.

I need to:

  1. If <h2> has <a> tag then, check if it has id. if it's not there then set the id
  2. If <h2> tag donesn't have <a> tag then add <a> tag in <h2>

e.g. if original HTML is like this: Case 1:

<h2><a href="#headingABC"><span style="color:#009bd2;">My Heading ABC</span></a></h2>

Then, change it to (add id):

<h2><a id="heading1" href="#headingXYZ"><span style="color:#009bd2;">My Heading ABC</span></a></h2>

case 2:

<h2><span style="color:#009bd2;">My Heading ABC</span></h2>

Then, change it to (add a tag by moving all elements in h2 inside a):

<h2><a id="heading1"><span style="color:#009bd2;">My Heading ABC</span></a></h2>

How can I achieve this using JSOUP in Java (version 11).


Solution

  • Elements elements = doc.select("h2");
    for (Element element : elements) {
        Element firstChild = element.child(0);
        if (firstChild.tagName().equals("a") && firstChild.attr("id").isEmpty()) {
            firstChild.attr("id", "heading1");
        } else {
            firstChild.wrap("<a id=\"heading1\">");
        }
    }