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

Related Questions

Given a simple program such as the following, how would you: compile it as
Given the following SUT, would you consider this unit test to be unnecessary? **edit
Given the following sequence: normal %(b)BOLD% normal I want to transform this into the
Given the following jquery code.. jQuery.fn.sliding = function () { return this.each(function () {
Given the following: StreamLogger& operator<<(const char* s) { elements.push_back(String(s)); return *this; } StreamLogger& operator<<(int
This is just an example, but given the following model: class Foo(models.model): bar =
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
I have this timer function, it gives me following exception. Collection was modified; enumeration
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
Given the following CSS3 animation.... <style type=text/css media=screen> .drop_box { -webkit-animation-name: drop; -webkit-animation-duration: 2s;

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.