【问题标题】:HTML to formatted textHTML 到格式化文本
【发布时间】:2012-03-11 07:28:33
【问题描述】:

是否有任何 java API 可以像在 Android 中那样执行类似 Html.fromHtml() 的操作? 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

    一个。测试A1

    b.测试B1

  2. 测试2

    一个。测试A2

    b.测试B2

【问题讨论】:

  • 我认为一些 JComponet 可以呈现 HTML,如果那是你正在寻找的。​​span>
  • 我希望将一些类似的 html 标签转换为普通格式的文本到文件
  • 那你可能就靠自己了。查看 StringTokenizer 类。
  • 你可以用jsoup做到这一点,但你必须自己构造列表。

标签: java android html jsoup


【解决方案1】:

没有 api 用于jsoup-to-"formated text",但您可以自行转换列表:

  1. 迭代遍历作为列表根的ul/ol元素的所有childs
  2. if item:格式化并添加输出字符串
  3. 如果 sublist:执行 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();
}

测试代码:

    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

就是这样! :-)

【讨论】:

  • 谢谢你。当我发布问题但没有得到答案时,我做了类似的事情。不过很好的实现!
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2011-12-09
相关资源
最近更新 更多