我在编写递归函数时遇到问题,该函数从xml文件中检索信息并生成树结构。它应该以递归方式完成。请帮助我,我被困在这一天,每天花费近6个小时。谢谢。
问题是我得到了这个例外: IllegalArgumentException:新子女是祖先
并且它是导致它的递归调用。
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)
答案 0 :(得分:0)
问题是你正在尝试将Node添加为自己的子节点,这是不允许的。你需要这样做:
} else {
retNode.add(readNode(sc)); // line 98 <-- this will recursively add nodes beneath this one
return retNode;
}