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

  • Home
  • SEARCH
  • 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 345847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:07:20+00:00 2026-05-12T11:07:20+00:00

Given the following: > This is level 1 > This is level 2 >>

  • 0

Given the following:

  > This is level 1
  > This is level 2
  >> This is level 2.1
  >> This is level 2.2
  >>> This is level 2.2.1
  >>> This is level 2.2.2
  > This is level 3

How would you convert that text to XHTML, without a parser library such as ANTLR? That is:

  <ul>
  <li>This is level 1</li>
  <li>This is level 2
    <ul>
    <li>This is level 2.1</li>
    <li>This is level 2.2
      <ul>
      <li>This is level 2.2.1</li>
      <li>This is level 2.2.2</li>
      </ul>
    </li>
    </ul>
  </li>
  <li>This is level 3</li>
  </ul>

I have tried both recursive and iterative algorithms. The troubling part is closing the ul tags from depth 3 (2.2.2) to depth 1 (3).

Solution

The following code solves the problem. The solution marked as correct was correct when each level represented a single number, rather than a line of text. New lines in the output are for human readability, but since (X)HTML is computer-read, they have been removed from the code below.

public String transform( String source ) {
  // Level 0 means no >, level 1 for one >, etc.
  //
  int currentLevel = 0;
  int nextLevel = 0;

  StringBuilder sb = new StringBuilder( 512 );

  // Split source on newlines.
  //
  String[] lines = source.split( "\\r?\\n" );

  for( String line: lines ) {
    int indents = line.lastIndexOf( ">" );

    if( indents < 0 ) {
      continue;
    }

    String content = line.substring( indents + 1 ).trim();

    nextLevel = indents + 1;

    if( nextLevel == currentLevel ) {
      sb.append( "</li><li>" );
    }
    else if( nextLevel > currentLevel ) {
      sb.append( "<ul><li>" );
    }
    else if( nextLevel < currentLevel ) {
      for( int i = 0; i < currentLevel - nextLevel; i++ ) {
        sb.append( "</li></ul>" );
      }
      sb.append( "</li><li>" );
    }

    sb.append( content );

    currentLevel = nextLevel;
  }

  // Close the remaining levels.
  //
  for( int i = 0; i < currentLevel; i++ ) {
    sb.append( "</li></ul>" );
  }

  return sb.toString();
}
  • 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-05-12T11:07:20+00:00Added an answer on May 12, 2026 at 11:07 am

    Here goes a sample implementation based on Pavel’s algorithm

    class listCreator {
    
        public String createList(String source) {
            int currentLevel = 0; //Level 0 means beginning, level 1 means a single > was present and so on
            int nextLevel = 0;
            StringBuilder sb = new StringBuilder();
            //Assumes source is to be split on newlines
            String[] tmp = source.split("\n");
            for (String t: tmp) {
                //Needs validation, if source is not what we expect it'll blow up...
                //We are expecting a number of > followed by a space
                String[] levelContent = t.split(" ");
                nextLevel = levelContent[0].lastIndexOf(">") + 1;
    
                if (nextLevel == currentLevel) {
                    sb.append("</li>\n<li>");
                    sb.append(levelContent[1]);
                } else if (nextLevel > currentLevel) {
                    sb.append("<ul>\n<li>");
                    sb.append(levelContent[1]);
                } else if (nextLevel < currentLevel) {
                    for (int i = 0; i < currentLevel-nextLevel; i++) {
                        sb.append("</li>\n</ul>\n");
                    }
                    sb.append("</li>\n<li>");
                    sb.append(levelContent[1]);
                }
    
                currentLevel = nextLevel;
            }
            //Close up remaining levels
            for (int i=0; i < currentLevel; i++) {
                sb.append("</li>\n</ul>\n");
            }
            return sb.toString();
        }
    
        public static void main(String[] args) {
            String source1 = "> 1\n> 2\n>> 2.1\n>> 2.2\n>>> 2.2.1\n>>> 2.2.2\n> 3\n";
            String source2 = "> 1\n> 2\n>> 2.1\n>> 2.0.1\n>>> 2.0.1.2\n>> 2.2\n>>> 2.2.1\n>>> 2.2.2\n> 3\n";
            listCreator lc = new listCreator();
            System.out.println(lc.createList(source1));
            System.out.println(lc.createList(source2));
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 168k
  • Answers 168k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should definitely use instruments to debug this issue (unless… May 12, 2026 at 1:43 pm
  • Editorial Team
    Editorial Team added an answer You could use apreq_encode() from libapreq2. Note that your write… May 12, 2026 at 1:43 pm
  • Editorial Team
    Editorial Team added an answer http://mail.google.com/support/bin/answer.py?hl=en&answer=12119 http://mail.google.com/support/bin/topic.py?hl=en&topic=12867 I'll re-articulate my answer if you re-articulate your… May 12, 2026 at 1:43 pm

Related Questions

Given the following: > This is level 1 > This is level 2 >>
I am new to the Oracle 10g Resource Manager and am looking for guidance
I'm using the low-level API in Google App Engine for Java and want to
The issue I have is as follows: My company's supplier gives us an Access
How do you find the smallest unused number in a SQL Server column? I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.