漂亮的HTML片段输出 [英] Pretty HTML snippet output

查看:102
本文介绍了漂亮的HTML片段输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML < div>< p> text1< / p>< / div>< div>< p> text1< / p>< div>

我想让它变成这样

<div>
  <p>text1</p>
</div>
<div>
  <p>text1</p>
</div>

最简单的方法是什么? (我已经看过了变换和jsoup),但不确定使用什么会非常聪明。谢谢!

What would be most simple way to do it? (I've looked on transform and jsoup) but not sure what would be really smart to use. Thanks!

推荐答案

jTidy可以适合这项任务 - http://jtidy.sourceforge.net/howto.html

jTidy could fit for this task - http://jtidy.sourceforge.net/howto.html

public String prettyPrintHTML(String rawHTML)
{    
    Tidy tidy = new Tidy();
    tidy.setXHTML(true);
    tidy.setIndentContent(true);
    tidy.setPrintBodyOnly(true);
    tidy.setTidyMark(false);

    // HTML to DOM
    Document htmlDOM = tidy.parseDOM(new ByteArrayInputStream(rawHTML.getBytes()), null);

    // Pretty Print
    OutputStream out = new ByteArrayOutputStream();
    tidy.pprint(htmlDOM, out);

    return out.toString();
}

这篇关于漂亮的HTML片段输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆