[Java] JTree访问XML →→→→→进入此内容的聊天室

来自 , 2020-03-18, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/81c650ca
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package yan.t1;
  7.  
  8. import java.awt.BorderLayout;
  9. import java.awt.GridLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.IOException;
  13. import javax.swing.JButton;
  14. import javax.swing.JFileChooser;
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTree;
  20. import javax.swing.filechooser.FileNameExtensionFilter;
  21. import javax.swing.tree.*;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import org.w3c.dom.*;
  26. import org.xml.sax.SAXException;
  27.  
  28.  
  29. /**
  30.  * XmlJTree class
  31.  * @author Ibrabel
  32.  */
  33. public final class XmlJTree extends JTree{
  34.  
  35.     DefaultTreeModel dtModel=null;
  36.  
  37.     /**
  38.      * XmlJTree constructor
  39.      * @param filePath
  40.      */
  41.     public XmlJTree(String filePath){
  42.         if(filePath!=null)
  43.         setPath(filePath);
  44.     }
  45.  
  46.     public void setPath(String filePath){
  47.         Node root = null;
  48.         /*
  49.             Parse the xml file
  50.         */
  51.         try {
  52.             DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  53.             DocumentBuilder builder = factory.newDocumentBuilder();
  54.             Document doc = builder.parse(filePath);
  55.             root = (Node) doc.getDocumentElement();
  56.         } catch (IOException | ParserConfigurationException | SAXException ex) {
  57.             JOptionPane.showMessageDialog(null,"Can't parse file",
  58.                             "Error", JOptionPane.ERROR_MESSAGE);
  59.             return;
  60.         }
  61.         /*
  62.             if any result set the appropriate model to the jTree
  63.         */
  64.         if(root!=null){
  65.             dtModel= new DefaultTreeModel(builtTreeNode(root));
  66.             this.setModel(dtModel);
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * fullTreeNode Method
  72.      * construct the full jTree from any xml file
  73.      * this method is recursive
  74.      * @param root
  75.      * @return DefaultMutableTreeNode
  76.      */
  77.     private DefaultMutableTreeNode builtTreeNode(Node root){
  78.         DefaultMutableTreeNode dmtNode;
  79.  
  80.         dmtNode = new DefaultMutableTreeNode(root.getNodeName());
  81.             NodeList nodeList = root.getChildNodes();
  82.             for (int count = 0; count < nodeList.getLength(); count++) {
  83.                 Node tempNode = nodeList.item(count);
  84.                 // make sure it's element node.
  85.                 if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
  86.                     if (tempNode.hasChildNodes()) {
  87.                         // loop again if has child nodes
  88.                         dmtNode.add(builtTreeNode(tempNode));
  89.                     }
  90.                 }
  91.             }
  92.         return dmtNode;
  93.     }
  94.  
  95.     public static void main(String[] args) {
  96.         /*
  97.             Create simple frame for the example
  98.         */
  99.         JFrame myFrame = new JFrame();
  100.         myFrame.setTitle("XmlJTreeExample");
  101.         myFrame.setSize(300, 500);
  102.         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103.         myFrame.setLocationRelativeTo(null);
  104.         JPanel pan = new JPanel(new GridLayout(1, 1));
  105.         /*
  106.             Add jTree
  107.         */
  108.         final XmlJTree myTree = new XmlJTree(null);
  109.         myFrame.add(new JScrollPane(myTree));
  110.         /*
  111.             Add a button to choose the file
  112.         */
  113.         JButton button = new JButton("Choose file");
  114.         button.addActionListener(new ActionListener(){
  115.  
  116.             @Override
  117.             public void actionPerformed(ActionEvent ae) {
  118.                 JFileChooser chooser = new JFileChooser();
  119.                 FileNameExtensionFilter filter = new FileNameExtensionFilter(
  120.                         "XML file", "xml");
  121.                 chooser.setFileFilter(filter);
  122.                 int returnVal = chooser.showOpenDialog(null);
  123.                 if(returnVal == JFileChooser.APPROVE_OPTION) {
  124.                     myTree.setPath(chooser.getSelectedFile().getAbsolutePath());
  125.                 }
  126.             }
  127.         });
  128.         pan.add(button);
  129.         /*
  130.             Add the JPanel to the JFrame and set the JFrame visible
  131.         */
  132.         myFrame.add(pan,BorderLayout.SOUTH);
  133.         myFrame.setVisible(true);
  134.     }
  135. }
  136.  

回复 "JTree访问XML"

这儿你可以回复上面这条便签

captcha