//Source code mang tính tham khảo cho các bạn đang làm đề tài xml bằng java.[SPOILER] Mã: [LEFT][FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]javax.swing.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]java.awt.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]java.awt.event.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]java.awt.Event.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]java.io.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]javax.swing.tree.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]org.xml.sax.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]org.xml.sax.helpers.*;[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]import [/B][/COLOR][COLOR=#000000]org.apache.xerces.parsers.*;[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]public class [/B][/COLOR][COLOR=#000000]XMLTreeView [/COLOR][COLOR=#000000]{[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]private [/B][/COLOR][COLOR=#000000]SAXTreeBuilder saxTree = [/COLOR][COLOR=#7f0055][B]null[/B][/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]private static [/B][/COLOR][COLOR=#000000]String file = [/COLOR][COLOR=#2a00ff]""[/COLOR][COLOR=#000000];[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]public static [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]main[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]String args[/COLOR][COLOR=#000000][]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]JFrame frame = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]JFrame[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2a00ff]"XMLTreeView: [ games.xml ]"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]frame.setSize[/COLOR][COLOR=#000000]([/COLOR][COLOR=#990000]400[/COLOR][COLOR=#000000],[/COLOR][COLOR=#990000]400[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]frame.addWindowListener[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]WindowAdapter[/COLOR][COLOR=#000000](){[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]windowClosing[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]WindowEvent ev[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]System.exit[/COLOR][COLOR=#000000]([/COLOR][COLOR=#990000]0[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]})[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]file = [/COLOR][COLOR=#2a00ff]"games.xml"[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]XMLTreeView[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]frame[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#000000]XMLTreeView[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]JFrame frame[/COLOR][COLOR=#000000]){ [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]frame.getContentPane[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000].setLayout[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]BorderLayout[/COLOR][COLOR=#000000]())[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]DefaultMutableTreeNode top = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]// DefaultMutableTreeNode top = new DefaultMutableTreeNode("XML Document"); [/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]saxTree = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]SAXTreeBuilder[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]top[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]try [/B][/COLOR][COLOR=#000000]{ [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]SAXParser saxParser = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]SAXParser[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]saxParser.setContentHandler[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]saxTree[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]saxParser.parse[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]InputSource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]FileInputStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file[/COLOR][COLOR=#000000])))[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][COLOR=#7f0055][B]catch[/B][/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]Exception ex[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]top.add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ex.getMessage[/COLOR][COLOR=#000000]()))[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]JTree tree = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]JTree[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]saxTree.getTree[/COLOR][COLOR=#000000]())[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]JScrollPane scrollPane = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]JScrollPane[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]tree[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]frame.getContentPane[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000].add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2a00ff]"Center"[/COLOR][COLOR=#000000],scrollPane[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]frame.setVisible[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]true[/B][/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]} [/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]class [/B][/COLOR][COLOR=#000000]SAXTreeBuilder [/COLOR][COLOR=#7f0055][B]extends [/B][/COLOR][COLOR=#000000]DefaultHandler[/COLOR][COLOR=#000000]{[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]private [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode currentNode = [/COLOR][COLOR=#7f0055][B]null[/B][/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]private [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode previousNode = [/COLOR][COLOR=#7f0055][B]null[/B][/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]private [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode rootNode = [/COLOR][COLOR=#7f0055][B]null[/B][/COLOR][COLOR=#000000];[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#000000]SAXTreeBuilder[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]DefaultMutableTreeNode root[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]rootNode = root;[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]startDocument[/COLOR][COLOR=#000000](){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]currentNode = rootNode;[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]endDocument[/COLOR][COLOR=#000000](){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]characters[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]char[/B][/COLOR][COLOR=#000000][] [/COLOR][COLOR=#000000]data,int start,int [/COLOR][COLOR=#7f0055][B]end[/B][/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]String str = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]String[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]data,start,end[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]if [/B][/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]!str.equals[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2a00ff]""[/COLOR][COLOR=#000000]) [/COLOR][COLOR=#000000]&& Character.isLetter[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]str.charAt[/COLOR][COLOR=#000000]([/COLOR][COLOR=#990000]0[/COLOR][COLOR=#000000])))[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]currentNode.add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]str[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]startElement[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]String uri,String qName,String lName,Attributes atts[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]previousNode = currentNode;[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]currentNode = [/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]lName[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]// Add attributes as child nodes //[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]attachAttributeList[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentNode,atts[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]previousNode.add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentNode[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]endElement[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]String uri,String qName,String lName[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]if [/B][/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]currentNode.getUserObject[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000].equals[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]lName[/COLOR][COLOR=#000000]))[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]currentNode = [/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]currentNode.getParent[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]public [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode getTree[/COLOR][COLOR=#000000](){[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]return [/B][/COLOR][COLOR=#000000]rootNode;[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#7f0055][B]private [/B][/COLOR][COLOR=#7f0055][B]void [/B][/COLOR][COLOR=#000000]attachAttributeList[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]DefaultMutableTreeNode node,Attributes atts[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#7f0055][B]for [/B][/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]int [/B][/COLOR][COLOR=#000000]i=[/COLOR][COLOR=#990000]0[/COLOR][COLOR=#000000];i<atts.getLength[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000];i++[/COLOR][COLOR=#000000]){[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]String name = atts.getLocalName[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]String value = atts.getValue[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]name[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]node.add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#7f0055][B]new [/B][/COLOR][COLOR=#000000]DefaultMutableTreeNode[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]name + [/COLOR][COLOR=#2a00ff]" = " [/COLOR][COLOR=#000000]+ value[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000];[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#000000]}[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]//games.xml[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]/*[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]<?xml version="1.0"?>[/COLOR][/FONT][/LEFT] [LEFT][FONT=Courier New][COLOR=#3f7f5f]<games>[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]<game genre="rpg">XML Invaders</game>[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]<game genre="rpg">A Node in the XPath</game>[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]<game genre="rpg">XPath Racers</game>[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]</games>[/COLOR][/FONT] [FONT=Courier New][COLOR=#3f7f5f]*/[/COLOR][/FONT][/LEFT] [/SPOILER] Nguồn: Java2s.
mình có thể làm mất các biểu tưởng cây thư mục được ko và các tên thư mục có thể biến thành các link ko bạn làm như các menu của website 24h đó
Để xóa các biểu tượng thì : Mã: [B]DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer();[/B] [B] renderer2.setOpenIcon(null);[/B] [B] renderer2.setClosedIcon(null);[/B] [B] renderer2.setLeafIcon(null);[/B] [I]tree[/I][B].setCellRenderer(renderer2);[/B] Hay tham khảo tại mục 5 ở đây.