Java HTML / XML How to - Remove tags from a string using JSOUP








Question

We would like to know how to remove tags from a string using JSOUP.

Answer

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
// ww w .  j  a  va  2  s  .com
public class Main {
  public static void main(String[] args) throws Exception {
    String xmlStr = "<style>" + //
        "v\\:* {behavior:url(#default#VML);}" + //
        "o\\:* {behavior:url(#default#VML);}" + //
        "w\\:* {behavior:url(#default#VML);}" + //
        ".shape {behavior:url(#default#VML);}" + //
        "</style>" + //
        "<xml>" + //
        "<w:WordDocument>" + //
        "<w:View>Normal</w:View>" + //
        "<w:Zoom>0</w:Zoom>" + //
        "<w:TrackMoves>false</w:TrackMoves>" + //
        "</xml>";
    Document doc = Jsoup.parse(xmlStr, "", Parser.xmlParser());
    doc.select("style").remove();
    System.out.println(doc);
  }
}