I created a JTree from an XML file like this:
//Load Tree from XML file
File stocks = new File("//Users/scott/Desktop/saSample.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
// Set root folder
DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode(doc.getDocumentElement().getAttribute("saName"));
DefaultTreeModel treeModel = new DefaultTreeModel(rootnode);
objMgrTree.setModel(treeModel);
// Set Pages as sub-folders
NodeList nodes = doc.getElementsByTagName("Page");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
DefaultMutableTreeNode pagenode = new DefaultMutableTreeNode(node.getAttributes().getNamedItem("saName").getNodeValue());
rootnode.add(pagenode);
//Iterate through each page element to find all elements under it
NodeList subnodes = node.getChildNodes();
for (int j = 0; j < subnodes.getLength(); j++) {
Node subnode = subnodes.item(j);
if (subnode.getNodeType() == Node.ELEMENT_NODE) {
DefaultMutableTreeNode elementnode = new DefaultMutableTreeNode(subnode.getAttributes().getNamedItem("saName").getNodeValue());
pagenode.add(elementnode);
}
}
}
Now I'd like to refer back to the original XML node when an item is selected in the JTree (so I can get additional properties). How can I do that?
Thanks,
-Scott