Converting a list into XML

Viewed 1084

For a project I'm currently working on, I'm given an array of objects, each of which contain a "content" property and a "level" property. I need to convert this list into an HTML bulleted list. For example, if I was given the following input (shown in JSON for simplicity):

[ {content: "Hey", level: "1"},
  {content: "I just met you", level: "2"},
  {content: "and this is crazy", level: "2"},
  {content: "but here's my number", level: "1"},
  {content: "call me, maybe", level: "3"} ]

I would need to convert it to the following XHTML:

<ul>
  <li>Hey</li>
  <li>
    <ul>
      <li>I just met you</li>
      <li>and this is crazy</li>          
    </ul>
  </li>
  <li>but here's my number</li>
  <li>
    <ul>
      <li>
        <ul>
          <li>call me, maybe</li>      
        </ul>
      </li>
    </ul>
  </li>
</ul>

The end product would look like this:

  • Hey
    • I just met you
    • and this is crazy
  • but here's my number
    • call me, maybe (<- one level deeper - I don't think I can do this in SO)

Kind of a weird puzzle. Any suggestions as to an algorithm/approach that would be most efficient/easy to implement? I'm implementing this in C#, but an example/idea in another language would be more than welcome.

1 Answers
Related