简体   繁体   English

Java:从xml文件生成树

[英]Java: Generating a tree from xml file

I am having trouble with writing a recursive function that retrieves information from an xml file and produces a tree structure. 我在编写递归函数时遇到麻烦,该递归函数从xml文件中检索信息并产生树形结构。 It is supposed to be done recursively. 它应该以递归方式完成。 Please help me I have been stuck on this for a week putting in almost 6 hours a day. 请帮助我,我每天坚持将近6个小时来坚持一个星期。 Thanks. 谢谢。

The problem is that I get this exception: IllegalArgumentException: new child is an ancestor 问题是我得到了以下异常:IllegalArgumentException:新孩子是祖先

and it is the recursive call that is causing it. 导致它的是递归调用。

This is my code: 

   public class MyNode extends DefaultMutableTreeNode {
    private String level;
    private String name;
    private String text;

    public MyNode(String level, String name, String text){
        super(name);
        this.level = level;
        this.text = text;
       }

    }

--------

    public MyLifeTree_2() {
        Container c = getContentPane();
        //*** Build the tree and a mouse listener to handle clicks

        try{
            sc = new Scanner(new File("src/Liv.xml"));
        } catch(Exception e){
            System.out.println("Error: " + e.toString());
        }

        root = readNode(sc); // line 30

        treeModel = new DefaultTreeModel( root );
        tree = new JTree( treeModel );
        MouseListener ml =
                new MouseAdapter() {
                    public void mouseClicked( MouseEvent e ) {
                        if ( box.isSelected() )
                            showDetails( tree.getPathForLocation( e.getX(),
                                    e.getY() ) );
                    }
                };
        tree.addMouseListener( ml );
        //*** build the tree by adding the nodes
        //*** panel the JFrame to hold controls and the tree
        controls = new JPanel();
        box = new JCheckBox( showString );
        init(); //** set colors, fonts, etc. and add buttons
        c.add( controls, BorderLayout.NORTH );
        c.add( tree, BorderLayout.CENTER );
        setVisible( true ); //** display the framed window
    }


    private MyNode readNode(Scanner sc){
        MyNode retNode = null;

        if(sc.hasNext()){
            sc.nextLine();
            String line = sc.nextLine();
            Scanner ls = new Scanner(line);
            String w = ls.next();
            String level = w.substring(1);
            System.out.println(level);
            w =  ls.next();
            String name = w.substring(6,w.length()-2);
            System.out.println(name);
            String text = ls.next();
            System.out.println(text);
            retNode = new MyNode(level, name, text);
            String sw = sc.nextLine();
            if(sw.startsWith("</")){
                sc.nextLine();
                return retNode;
            } else {
                readNode(sc);
                retNode.add(retNode); // line 98
            }
        }
        return retNode;
    }

-----

    <?xml version="1.0" encoding="UTF-8"?>
    <PremierLeague name="PremierLeague"> this is the PM
    <Div1 name="Div1"> is under PM
    <Teams name="team1"> has sth
    <Player name="name1"> plays attack
    </Player>
    <Player name="name2"> plays attack
    </Player>
    </Teams>
    </Div1>
    </PremierLeague> 

PremierLeague
PremierLeague
this
Player
name1
plays
    Exception in thread "main" java.lang.IllegalArgumentException: new child is an ancestor
        at javax.swing.tree.DefaultMutableTreeNode.insert(DefaultMutableTreeNode.java:179)
        at javax.swing.tree.DefaultMutableTreeNode.add(DefaultMutableTreeNode.java:411)
        at MyLifeTree_2.readNode(MyLifeTree_2.java:98)
        at MyLifeTree_2.<init>(MyLifeTree_2.java:30)
        at MyLifeTree_2.main(MyLifeTree_2.java:160)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The issue is you're trying to add a Node as a child of itself, which isn't allowed. 问题是您试图将Node添加为其自身的子级,这是不允许的。 You need to do this instead: 您需要这样做:

} else {

    retNode.add(readNode(sc)); // line 98 <-- this will recursively add nodes beneath this one
    return retNode;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM