Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9254513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:29:19+00:00 2026-06-18T11:29:19+00:00

Are there any java APIs which does similar action like Html.fromHtml() as in Android?

  • 0

Are there any java APIs which does similar action like Html.fromHtml() as in Android? JSoup does parse and remove the tags but the output is not a formatted one.
eg:

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

should give me something like

  1. Test1

    a. TestA1

    b. TestB1

  2. Test2

    a. TestA2

    b. TestB2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:29:21+00:00Added an answer on June 18, 2026 at 11:29 am

    There’s no api for jsoup-to-“formated text”, but you can convert lists by your own:

    1. iterate over all childs of the ul / ol element which is the root of the list
    2. if item: format and add the output String
    3. if sublist: do 1. – but with the sublist element – and add the result

    Example:

    In this example i use the type attribute to determine what kind of bullet is required and use the character (!) to index the items. If there’s no proper attribute, char 1 is used.

    Implementation:

    /**
     * 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
    

    Result:

    Input (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>
    

    Output:

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

    Thats it! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any Java function or util class which does rounding this way: func(3/2)
Is there any functionality in Java for pausing the program like system(PAUSE); does in
Are there any Java APIs (or web services) to beautify/format/pretty-print the html, xml and
Is there any plan to leverage java 7 util.concurrent's ForkJoin APIs or, expose similar
Are there any APIs available in Java to query jobs? In other words, I
Is there any free java or android barcode (Code128 or upc) generating library for
Are there any Java API(s) which will provide plural form of English words (e.g.
Is there any Java lib that allows me to download web pages recursively given
Is there any java utility to convert string to hex value (integer) ?
Is there any Java profiler that allows profiling short-lived applications? The profilers I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.