繁体   English   中英

Jsoup.parse(String) - 不添加\ n

[英]Jsoup.parse(String) - doesn't add \n

我正在使用Jsoup 1.7.2。

当使用API Jsoup.parse(String)我看到输出Document对象在解析的HTML中添加了换行符(文本换行符,\\ n)。

例如:输入字符串是:

<html><body><p>aaa</p></body></html>

Document对象具有以下内容(当调用toString() ):

<html>
 <head></head>
 <body>
  <p>aaa</p>
 </body>
</html>

我对<body>元素感兴趣。 如何指示Jsoup不要用新行格式化输出? 我期待身体部分是: <body><p>aaa</p></body>

另一方面,当我有一个带换行符的HTML时,我希望它们保持不变。

试着这样做:

Document newDocument = Jsoup.parse(htmlString, StringUtils.EMPTY, Parser.htmlParser());
newDocument.outputSettings().escapeMode(EscapeMode.base);
/**
 * Need CharEncoding.US_ASCII and not UTF-8 so the special characters will be encoded properly,
 * but representation of such will change. For instance: &mdash; will be encoded as &#8212;
 */
newDocument.outputSettings().charset(CharEncoding.US_ASCII);
newDocument.outputSettings().prettyPrint(false); // this will make sure that it will not add line breaks

试试这个吧。 它的工作

    Document doc = Jsoup.parse(String);
    // This line will keep your Html in one line
    doc.outputSettings().prettyPrint(false);

    System.out.println(doc.html());

暂无
暂无

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

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