qq_37878096
橙汁不加冰
采纳率25%
2017-08-23 04:32 阅读 2.0k
已采纳

关于格式化HTML代码字符串的问题

5

我写了一个for循环用以格式化代码并将其建立成树,可是总是显示不出下面的子节点是怎么回事,求高人解答
public class FormattedCode1 {

private static String getCount (int count){
    String st = "\n";
    if(count<0){
        count = 0;
    }
    for(int i=0;i<count;i++){
        st=st+"  ";
    }
        return st;
}

public static String Method_2(String jaq){
    int a = 0;
    String sr =""  ;
    String[] arr = {"1"};
    String[] st = jaq.split("<");
    String str = "";
    List<String> list =  Arrays.asList(st);
    System.out.println(list.toString());


    DefaultMutableTreeNode node[] = new DefaultMutableTreeNode[list.size()];

    node[0] = new DefaultMutableTreeNode("Code",true);
    JFrame f = new JFrame("HTML.code");   
    JPanel p;   
    JTree jTree5 = new JTree(node[0]); 

    for (int i = 1; i<list.size();i++) {
      char c = st[i].charAt(0); 
        if (((c>='a'&&c<='z')||(c>='A'&&c<='Z'))) {
            if (st[i].startsWith("img")||st[i].startsWith("link")||st[i].startsWith("meta")||st[i].startsWith("input")||st[i].startsWith("hr")||st[i].startsWith("br")) {
                if(st[i-1].startsWith("img")||st[i-1].startsWith("link")||st[i-1].startsWith("meta")||st[i-1].startsWith("input")||st[i-1].startsWith("hr")||st[i-1].startsWith("br")) {
                    a = a;//本级为单标签且上一级为单标签,等级为上一级等级

                }else if (st[i-1].startsWith("/")) {                        

                    a = a;//本级为单标签且上一级为结束标签,等级为上一级等级

                }else{                       

                    a = a+1;//本级为单标签且上一级不是单标签也不是结束标签,等级为上一级+1

                }
                }else{
                    if (st[i-1].startsWith("img")||st[i-1].startsWith("link")||st[i-1].startsWith("meta")||st[i-1].startsWith("input")||st[i-1].startsWith("hr")||st[i-1].startsWith("br")) {

                        a = a;//本级为开始标签,上一级为单标签,等级为上一级等级;

                    }else if (st[i-1].startsWith("/")) {                            

                        a = a;//本级为开始标签,上一级为结束标签,等级为上一级等级                            
                    }else{
                        //本级为开始标签,上一级为开始标签或为空,等级为上一级等级+1

                        ************node[i] = new DefaultMutableTreeNode(st[i],true);                             
                        node[a].add(new  DefaultMutableTreeNode(node[i]));

                        p = new JPanel();  
                        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));  
                        p.setPreferredSize(new Dimension(700, 400));  
                        p.add(new JScrollPane(jTree5));

                        f.setContentPane(p);  
                        f.pack();  
                        f.setLocation(300, 200);  
                        f.setVisible(true);  
                        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                         a = a+1;************


                    }
                }
            }
        if (st[i].startsWith("/")){
            if (st[i-1].startsWith("img")||st[i-1].startsWith("link")||st[i-1].startsWith("meta")||st[i-1].startsWith("input")||st[i-1].startsWith("hr")||st[i-1].startsWith("br")) {
                a = a-1; //本级为结束标签且上一级为单标签,等级为上一级等级-1

            }else if(st[i-1].startsWith("/")) {
                a = a-1;//本级为结束标签且上一级为结束标签,等级为上一级等级-1

            }else{
                a = a;//本级为结束标签等级为上一级非单标签且非结束标签,等级为上一级等级

            }               
        }   
        sr = getCount(a)+"<"+st[i];
        str+=(String)sr;

    }

    System.out.println(str);
    return str;  

}   

public static void main(String[] args) {
    String jaq = "<html><button><option><html></div><div><div><img><img></div></div></html></html></html></html>";
    FormattedCode.Method_2(jaq);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

相关推荐