Skip to content Skip to sidebar Skip to footer

Update A Tag Name Along With Its Value

I am trying to replace html tags with updated values. I had tried using JSOUP but could not work out a way yet. The functionality: if (webText.contains('a href')) { //

Solution 1:

You can use the replaceWith method of class Element. I've cleared your code a little bit. Removed the arrays and used the provided lists wherever possible. Moreover you don't need regex to get the href attribute (or any other attribute for that matter) when you've already parsed the html. Check it out and inform me if you need further assistance.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {

        String webText = 
                "<html>" + 
                        "<head></head>" + 
                        "<body>" +
                            "<p dir=\"ltr\">" +
                                "<a href=\"gYWMBi5XqN\" class=\"favourite\"></a>" +
                                "<a href=\"gYWMBi5XqN\"><font color=\"#009a49\">Frank Frank</font></a>" +
                                "<a href=\"http://yahoo.co.in\" class=\"link\"></a>" +
                                "<a href=\"http://yahoo.co.in\"><font color=\"#0033cc\">http://yahoo.co.in</font></a>" + 
                            "</p>" + 
                        "</body>" + 
                    "</html>";

        if (webText.contains("a href")) {
            // Parse it into jsoup
            Document doc = Jsoup.parse(webText);

            Elements links = doc.select("a");

            for (Element link : links) {
                if (link.attr("href").contains("http")) {
                    System.out.println("Link: " + link.toString());
                    String url = link.attr("href");
                    if (url != null) {
                        System.out.println("Link Value: " + url);
                        Attributes attributes = new Attributes();
                        attributes.put("href", url);
                        attributes.put("class", "link");
                        link.replaceWith(new Element(Tag.valueOf("a"), "", attributes).insertChildren(0, link.childNodes()));       
                    }
                } else {
                    System.out.println("Favourite: " + link.toString());
                    String url = link.attr("href");
                    if (url != null) {
                        System.out.println("Favourite Value: " + url);
                        Attributes attributes = new Attributes();
                        attributes.put("href", url);
                        attributes.put("class", "favourite");
                        link.replaceWith(new Element(Tag.valueOf("a"), "", attributes).insertChildren(0, link.childNodes()));      
                    }
                }
            }

            Element element = doc.body();
            System.out.println("From element html *************** "+ element.html());
        }
    }
}

Input

<p dir="ltr">
    <a href="gYWMBi5XqN" class="favourite"></a>
    <a href="gYWMBi5XqN"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in" class="link"></a>
    <a href="http://yahoo.co.in"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

Output

<p dir="ltr">
    <a href="gYWMBi5XqN" class="favourite"></a>
    <a href="gYWMBi5XqN" class="favourite"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in" class="link"></a>
    <a href="http://yahoo.co.in" class="link"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

Input

<p dir="ltr">
    <a href="gYWMBi5XqN"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

Output

<p dir="ltr">
    <a href="gYWMBi5XqN" class="favourite"><font color="#009a49">Frank Frank</font></a>
    <a href="http://yahoo.co.in" class="link"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

Post a Comment for "Update A Tag Name Along With Its Value"