简体   繁体   English

无法删除 <xml> 和 <style> tags from a string using JSOUP

[英]Unable to remove <xml> and <style> tags from a string using JSOUP

I have a string which contains the following html code: 我有一个包含以下html代码的字符串:

 ...
 ...
 <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>
...
...

and when i apply 当我申请时

string.select("style").remove();
string.select("xml").remove();

nothing seems to happen. 似乎什么也没发生。 What i am doing wrong? 我做错了什么? I want to completely remove those tags including their content. 我想完全删除那些标签,包括其内容。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

public class Main {

    public static void main(String[] args) {
        try {
            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);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OUTPUT 输出值

<xml>
 <w:worddocument>
  <w:view>
   Normal
  </w:view>
  <w:zoom>
   0
  </w:zoom>
  <w:trackmoves>
   false
  </w:trackmoves>
 </w:worddocument>   //Added by jsoup during normalization of the xml document
</xml>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM