I've loaded a JTree with nodes from an XML Schema file using the XSOM API (https://xsom.dev.java.net).
Every time a file is selected I do the folowing:
schemaParser = new XSDParser(selectedFile.getAbsolutePath());
TreeModel model = schemaParser.generateTreeModel();
schemaTree.setModel(model);
schemaTree.setCellRenderer(new SchemaTreeTraverser.SchemaTreeCellRenderer());
schemaTree is the variable name for the JTree.
The code for XSDParser is as follows:
package schemaparser;
import java.io.*;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.impl.util.SchemaTreeTraverser;
import com.sun.xml.xsom.impl.util.SchemaWriter;
import com.sun.xml.xsom.parser.XSOMParser;
import javax.swing.tree.TreeModel;
public class XSDParser {
private XSOMParser reader;
private XSSchemaSet xss;
public XSDParser(String parseFile){
try {
reader = new XSOMParser();
reader.parse(new File(parseFile));
xss = reader.getResult();
if (xss == null) {
System.out.println("error");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public TreeModel generateTreeModel() {
SchemaTreeTraverser stt = new SchemaTreeTraverser();
stt.visit(xss);
TreeModel model = stt.getModel();
System.out.println("Returning the tree model");
return model;
}
}
Now whenever a node is selected in the JTree I do the following:
private void schemaTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
TreePath path = schemaTree.getSelectionPath();
if(path != null)
System.out.println(path.toString());
}
However now no matter which node I select in the tree I get something like: [null, null, null, null] (depending on how far the hierarchy I go)
For a simple JTree this would usually print out the path to the node, e.g.: [JTree, colors, red]
Anyone any idea how to fix this?
Thanks, Patrick