java 实现JTree,其中DefaultMutableTreeNode从xml文件中读取

4xrmg8kj  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(137)

是否可以在JTree中创建,而不对每个树节点进行硬编码,而是从xml文件中阅读,并获得与以下代码相同的输出:

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class test {

test() {
    JFrame f = new JFrame("Swing");
    DefaultMutableTreeNode life = new DefaultMutableTreeNode("Life");
    DefaultMutableTreeNode plants = new DefaultMutableTreeNode("Plants");
    DefaultMutableTreeNode animals = new DefaultMutableTreeNode("Animals");
    DefaultMutableTreeNode cryptogamers = new DefaultMutableTreeNode("Cryptogamers");
    DefaultMutableTreeNode mammals = new DefaultMutableTreeNode("Mammals");
    JTree root = new JTree(life);

    life.add(plants);
    life.add(animals);

    plants.add(cryptogamers);
    animals.add(mammals);

    f.setSize(200, 200);
    f.add(root);
    f.setVisible(true);

    }

public static void main(String[] args) {
    new test();
    }

}

我希望使用我创建的XML文件生成相同的结果,但不对每个节点进行硬编码:

<Biosphere name="Life"> 
<Kingdom name="Plants"> 
<Division name="Cryptogamers"> 
</Division>
</Kingdom>
<Kingdom name="Animals"> 
<Division name="Mammals"> 
</Division>
</Kingdom>
</Biosphere>
s6fujrry

s6fujrry1#

如果你用XMLEncoder序列化这个树,你可以生成如下所示的东西。通过扩展,你可以编辑它,然后反序列化它。当然,这可以很好地压缩,因为有很多冗余。序列化看起来像:

public void serialize(TreeModel model) {
    try (XMLEncoder enc = new XMLEncoder(Files.newOutputStream(Path.of("tree.xml")))) {
        enc.writeObject(model);
    }
    catch(IOException e) {
        e.printStackTrace();    
    }
}

生产:

<java version="18.0.2.1" class="java.beans.XMLDecoder">
 <object class="javax.swing.tree.DefaultTreeModel">
  <object class="javax.swing.tree.DefaultMutableTreeNode">
   <void property="userObject">
    <string>Life</string>
   </void>
   <void method="add">
    <object class="javax.swing.tree.DefaultMutableTreeNode">
     <void property="userObject">
      <string>Plants</string>
     </void>
     <void method="add">
      <object class="javax.swing.tree.DefaultMutableTreeNode">
       <void property="userObject">
        <string>Cryptogamers</string>
       </void>
      </object>
     </void>
    </object>
   </void>
   <void method="add">
    <object class="javax.swing.tree.DefaultMutableTreeNode">
     <void property="userObject">
      <string>Animals</string>
     </void>
     <void method="add">
      <object class="javax.swing.tree.DefaultMutableTreeNode">
       <void property="userObject">
        <string>Mammals</string>
       </void>
      </object>
     </void>
    </object>
   </void>
  </object>
 </object>
</java>
xfb7svmp

xfb7svmp2#

有多种XML树表示,您可以将XML文档解析为DOM、Xdm、JDOM等,并通过递归处理该XML树来递归构建JTree,以下是使用Saxon HE和XdmNode的示例,例如:

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import java.io.File;

public class Main {
    Main(XdmNode inputDoc) {
        JFrame f = new JFrame("Swing");

        JTree root = new JTree(parseXdmTreeToSwingTree((XdmNode)inputDoc.children("*").iterator().next()));


        f.setSize(200, 200);
        f.add(root);
        f.setVisible(true);

    }

    MutableTreeNode parseXdmTreeToSwingTree(XdmNode inputNode) {
        DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(inputNode.attribute("name"));
        for (XdmNode child : inputNode.children( "*"))
            treeNode.add(parseXdmTreeToSwingTree(child));
        return treeNode;
    }

    public static void main(String[] args) throws SaxonApiException {
        Processor processor = new Processor(true);
        XdmNode inputDoc = processor.newDocumentBuilder().build(new File("sample1.xml"));
        new Main(inputDoc);
    }
}

撒克逊人他是在Maven例如。

<dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>11.4</version>
    </dependency>

会将当前版本11.4添加到项目中。

相关问题