开发者

Generating OL LI tree structure from a recursive call of Java method

I have list of objects type Node (items). And Node class has children in it (childItems). I Want to print this structure in ol li html markup.

Preparing in a recursive method. But there is some issue in my logic. Have a look on my code.

The the size of the items will be 2. According to given sample markup. Means 2 top level parents exist. And then they have further children as a list in them.

StringBuffer html = new StringBuffer();

 void printValues(ArrayList items){ 
for (Object o : items){
    html.append("<ol>");
    html.append("<li>");
    Node node = (Node)o;
    html.append(node.getName);

    if (node.getChildItems()!= null){
        printValues(node.getChildItems());
    }else{
        html.append("</li>");
    }
    html.append("</ol>");
}   
 }
// ...........
 System.out.println(html.toString(););

//...
  public class Node{
String Name;
ArrayList childItems = new ArrayList(); // of type Node
/* getter setters are proper */
}

Following markup is an example. It can be on N level.

<ol>
<li>
    Manager
    <ol>
        <li>
            Associate Manager
            <ol>
                <li>
                    A.M. Configuration 1
                </li>
                <li>
                    A.M. Configuration 2
                </li>
                <li>
                    Staff Memmber
                    <ol>
                        <li>
                            Staff Memmber Configuration
                        </li>
                        <!-- can goes on -->
                        <li>...</li>
                    </ol>
                </li>
            </ol>
        </li>
    </ol>
</li>
<li>
    Client Package
    <ol>
        <li>
            Gold
            <ol>
                <li>
                    Feature 1
                </li>
                <li>
                    Feature 2
                </li>
            </ol>
        </li>
   开发者_JAVA技巧 </ol>
</li>


Here is one example (my comment about functions calling each other):

public class LiOl {
  static StringBuilder sb = new StringBuilder();

  static void printList(List<Node> l) {
    if (l == null || l.size() == 0) {
      return;
    }
    sb.append("<ol>");
    for (Node n : l) {
      printNode(n);
    }
    sb.append("</ol>");
  }

  static void printNode(Node n) {
    sb.append("<li>").append(n.name).append("</li>");

    sb.append("<li>");
    printList(n.children);
    sb.append("</li>");
  }

  public static void main(String[] args) {
    List<Node> l = null;
    printList(l);

    sb.toString();
  }
}

class Node {
  String name;
  List<Node> children;
}


This part should be worked over:

...
html.append("<li>");    
Node node = (Node)o;    
html.append(node.getName);    
if (node.getChildItems()!= null){        
  printValues(node.getChildItems());    
}else{
  html.append("</li>");    
}
...

you'll see what I mean when we reduce it more to

...
html.append("<li>");    
...
if (node.getChildItems()!= null){        
  ...    
}else{
  html.append("</li>");    
}
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜