genevish -2 Junior Poster in Training

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);

        // Set root folder
        DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode(doc.getDocumentElement().getAttribute("saName"));
        DefaultTreeModel treeModel = new DefaultTreeModel(rootnode);

        // 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());

            //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());

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?



Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.