fsdfsdfsdfdasdasd"; Document doc = Jsoup.parse(html); Elem...">

Replace HTML tags using jsoup

Here is my code

String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>"; Document doc = Jsoup.parse(html); Elements elements = doc.select("font"); for(Element element : elements) { element.replaceWith(new Element(Tag.valueOf("span"),"").html(element.html())); } System.out.println(doc.html()); 

I want to replace the font tag and put the span tag. In this case, it will replace the first font tag, but not the second tag

+6
source share
1 answer

You can also replace the tag as follows:

 String html = "<font>fsdfs<font>dfsdf</font>dasdasd</font>"; Document doc = Jsoup.parse(html); Elements elements = doc.select("font"); // rename all 'font'-tags to 'span'-tags, will also keep attributs etc. elements.tagName("span"); System.out.println(doc.html()); 
+20
source

Source: https://habr.com/ru/post/925646/


All Articles