加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – HTML格式化文本

发布时间:2020-12-15 02:31:50 所属栏目:Java 来源:网络整理
导读:是否有任何类似于 Html.from Html()的 Java API在 Android中执行? JSoup确实解析并删除了标签,但输出不是格式化的. 例如: ol type="1" liTest1/li ol type="a" liTestA1/li liTestB1/li /ol liTest2/li ol type="a" liTestA2/li liTestB2/li /ol/ol 应该给
是否有任何类似于 Html.from Html()的 Java API在 Android中执行? JSoup确实解析并删除了标签,但输出不是格式化的.
例如:

<ol type="1">
 <li>Test1</li>
 <ol type="a">
  <li>TestA1</li>
  <li>TestB1</li>
 </ol>
 <li>Test2</li>
 <ol type="a">
  <li>TestA2</li>
  <li>TestB2</li>
 </ol>
</ol>

应该给我一些类似的东西

>测试1

一个. TestA1

湾TestB1
>测试2

一个. TestA2

湾TestB2

解决方法

jsoup到“格式化文本”没有api,但您可以自己转换列表:

>遍历ul / ol元素的所有子元素,这是列表的根
> if item:format并添加输出String
> if sublist:do 1. – 但是使用sublist元素 – 并添加结果

例:

在这个例子中,我使用type属性来确定需要什么类型的项目符号,并使用字符(!)来索引项目.如果没有合适的属性,则使用char 1.

执行:

/**
 * Convert the Listelement <code>root</code> to a formated string-representation.
 * 
 * @param root      Rootelement of the list (normally 'ul' or 'ol' tag)
 * @param depth     Depth of the list (<code>=0</code> for root element)
 * @return          List as String
 */
public String createList(Element root,int depth)
{
    final String indentation = createIndentation(depth); // create indentation
    StringBuilder sb = new StringBuilder();

    final String typeAttr = root.attr("type"); // Get the character used as bullet (= 'type' attribute)
    char type = typeAttr.isEmpty() ? '1' : typeAttr.charAt(0); // if 'type' attribute: use it,else: use '1' instead

    for( Element sub : root.children() ) // Iterate over all Childs
    {
        // If Java < 7: use if/else if/else here
        switch( sub.tagName() ) // Check if the element is an item or a sublist
        {
            case "li": // Listitem,format and append
                sb.append(indentation).append(type++).append(". ").append(sub.ownText()).append("n");
                break;
            case "ol": // Sublist
            case "ul":
                if( !sub.children().isEmpty() ) // If sublist is not empty (contains furhter items)
                {
                    sb.append(createList(sub,depth + 1)); // Recursive call for the sublist
                }
                break;
            default: // "Illegal" tag,do furhter processing if required - output as an example here
                System.err.println("Not implemented tag: " + sub.tagName());
        }
    }

    return sb.toString(); // Return the formated List
}


/**
 * Create an Indentationstring of <code>length</code> blanks.
 * 
 * @param length    Size of indentation
 * @return          Indentationstring
 */
private String createIndentation(int length)
{
    StringBuilder sb = new StringBuilder(length);

    for( int i=0; i<length; i++ )
    {
        sb.append(' ');
    }

    return sb.toString();
}

Testcode:

Document doc = ... // Load / parse your document here

    Element listRoot = doc.select("ol").first(); // Select the root-element (!) of the list here. 
    final String output = createList(listRoot,0); // Convert the list

    System.out.println(output); // Ouput

结果:

输入(HTML):

<ol type="1">
    <li>Test1</li>
    <ol type="a">
        <li>TestA1</li>
        <li>TestB1</li>
    </ol>
    <li>Test2</li>
    <ol type="a">
        <li>TestA2</li>
        <li>TestB2</li>
    </ol>
</ol>

输出:

1. Test1
 a. TestA1
 b. TestB1
2. Test2
 a. TestA2
 b. TestB2

而已!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读