HTML格式化文本

时间:2012-02-20 23:18:24

标签: java android html jsoup

是否有任何类似于Html.fromHtml()的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. 测试1

    一个。 TestA1

    湾TestB1

  2. 的Test2

    一个。 TestA2

    湾TestB2

1 个答案:

答案 0 :(得分:1)

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

  1. 迭代对作为列表根目录的ul / ol元素的所有孩子
  2. 如果 项目 :格式并添加输出字符串
  3. 如果 子列表 :执行 1。 - 但使用子列表元素 - 并添加结果
  4. 实施例

    在这个例子中,我使用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
    

    多数民众赞成! : - )