簡體   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