繁体   English   中英

jsoup解析html标签属性

[英]jsoup parse html tag attribute

例如:

<html>
   <head></head>
   <body sometag='"'></body>
</html>

当我使用Jsoup解析此类html时:

Document doc = Jsoup.parse(html);
doc.outputSettings().prettyPrint(false);
System.out.println(doc.toString());

它将成为

<html>
   <head></head>
   <body sometag="&quot;"></body>
</html>

注意'和',我不希望它解析'和',我只需要它来获取一些文本,有什么方法可以避免jsoup解析它。 非常感谢

只是不要使用HTML解析器。 请改用XML解析器。

Document doc = Jsoup.parse(html, "", Parser.xmlParser());

因此,我在使用不同的字符串转义时做了一些尝试,而实现此目的的最简单方法是执行以下操作:

尽管这可能不是您追求的目标,但我们会看到的。

String html = "<html> <head> </head> <body sometag='\"'> </body> </html>";

Document doc = Jsoup.parse(html);
doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
System.out.println( StringEscapeUtils.unescapeXml( doc.toString() ) );

暂无
暂无

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

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