Bei Einhaltung der Anleitung wird das Endprodukt (abh. von der XML-Datei, die ausgesen wird) wie folg aussehen:
Wir benötigen zwei Klassen:
Zuerst wird im GUI-Designer die JTree - Komponente angelegt. Im Sourcecode wird anschließend im Konstruktor nach der Methode initComponents() eine weitere Methode aufgerufen nämlich: initData():
public XMLTreeViewTopComponent() { initComponents(); setName(Bundle.CTL_XMLTreeViewTopComponent()); setToolTipText(Bundle.HINT_XMLTreeViewTopComponent()); initData(); }
Es werden die XML Daten geladen, um anschließend das TreeModel zu erstellen.
private void initData() { TreeNode treeNode = loadXML("C:/Users/wac2an/Desktop/test.xml"); TreeModel treeModel = new DefaultTreeModel(treeNode); jTree1.setModel(treeModel); }
private TreeNode loadXML(String xmlPath) { File xmlFile = new File(xmlPath); try { XMLDefaultHandler xmlHandler = new XMLDefaultHandler(); SAXParserFactory.newInstance().newSAXParser().parse(xmlFile, xmlHandler); return xmlHandler.getRoodeNode(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
In dieser Klasse kommt es zur Verarbeitung der XML-Daten in entsprechende Java-Objekte. Anfangs werden die Tags überprüft und angelegt sowie danach die Werte eingefügt.
public class XMLDefaultHandler extends DefaultHandler { /* * Creates an instance variable of TreeNode interface to represent * the root element */ TreeNode rootNode; /* * Creates an instance variable of MutableTreeNode interface to store * the child node */ MutableTreeNode childNode; /* * Creates an instance variable of MutableTreenode interface to store * the parent node */ MutableTreeNode parentNode; private Stack stckNodes = new Stack(); public TreeNode getRoodeNode() { return rootNode; } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //data stellt den jeweiligen Tag dar String data; if ("".equals(uri)) { data = qName; } else { data = "{" + uri + "}" + qName; } //Creates a DefaultMutableTreeNode instance and initializes it with the //valued stored in the variable data childNode = new DefaultMutableTreeNode(data); try { //Returns the node at the top of the stack without removing it parentNode = (MutableTreeNode) stckNodes.peek(); //Adds child to the parent at the specified location parentNode.insert(childNode, parentNode.getChildCount()); } catch (EmptyStackException e) { rootNode = childNode; } //Adds an element at the top of the node stckNodes.push(childNode); } public void characters(char[] ch, int start, int length) throws SAXException { //Obtains the data present in the node and stores it in //a String variable String s = new String(ch, start, length); System.out.println("content=" + s); //Creates an instance of DefaultMutableTreeNode and initializes //it with the value stored in the string variable MutableTreeNode dataObj = new DefaultMutableTreeNode(s); //Würde der Zeilenumbruch nicht abgefangen werden, wird der Zeilenumbruch //wie er in manchen XML-Dateien zur bessern Übersicht verwendet wird, ebenso //als wert weitergeben und es wird unnötigerweise ein leerer Knoten //angezeigt //Weiterhin werden mehrere Leerzeichen abgefangen wegen Einrückungen im XML //Dokument if (!s.equals("\n") && !s.contains(" ")) { //Adds the instance of DefaultMutableTreeNode at the specified location childNode.insert(dataObj, childNode.getChildCount()); } } public void endElement(String uri, String localName, String qName) throws SAXException { //Removes and returns the object at the start of the stack stckNodes.pop(); } }