XML Tree: Simple XML utility class : XML Tree « XML « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » XML » XML TreeScreenshots 
XML Tree: Simple XML utility class

import java.awt.Color;
import java.awt.Component;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class VSX2 {

  public TreeModel parse(String filenamethrows Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLIconTreeHandler handler = new XMLIconTreeHandler();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse(new File(filename), handler);
    return new DefaultTreeModel(handler.getRoot());
  }

  public static class XMLIconTreeHandler extends DefaultHandler {
    private DefaultMutableTreeNode root, currentNode;

    public DefaultMutableTreeNode getRoot() {
      return root;
    }

    public void startElement(String namespaceURI, String lName, String qName, Attributes attrs)
        throws SAXException {
      String eName = lName;
      if ("".equals(eName))
        eName = qName;
      ITag t = new ITag(eName, attrs);
      DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(t);
      if (currentNode == null) {
        root = newNode;
      else {
        currentNode.add(newNode);
      }
      currentNode = newNode;
    }

    public void endElement(String namespaceURI, String sName, String qNamethrows SAXException {
      currentNode = (DefaultMutableTreeNodecurrentNode.getParent();
    }

    public void characters(char buf[]int offset, int lenthrows SAXException {
      String s = new String(buf, offset, len).trim();
      ((ITagcurrentNode.getUserObject()).addData(s);
    }
  }

  public static class ITag implements IconAndTipCarrier {
    private String name;

    private String data;

    private Attributes attr;

    private Icon icon;

    private String tipText;

    public ITag(String n, Attributes a) {
      name = n;
      attr = a;
      for (int i = 0; i < attr.getLength(); i++) {
        String aname = attr.getQName(i);
        String value = attr.getValue(i);
        if (aname.equals("icon")) {
          tipText = value;
          icon = new ImageIcon(value);
          break;
        }
      }
    }

    public String getName() {
      return name;
    }

    public Attributes getAttributes() {
      return attr;
    }

    public void setData(String d) {
      data = d;
    }

    public String getData() {
      return data;
    }

    public String getToolTipText() {
      return tipText;
    }

    public Icon getIcon() {
      return icon;
    }

    public void addData(String d) {
      if (data == null) {
        setData(d);
      else {
        data += d;
      }
    }

    public String getAttributesAsString() {
      StringBuffer buf = new StringBuffer(256);
      for (int i = 0; i < attr.getLength(); i++) {
        buf.append(attr.getQName(i));
        buf.append("=\"");
        buf.append(attr.getValue(i));
        buf.append("\"");
      }
      return buf.toString();
    }

    public String toString() {
      String a = getAttributesAsString();
      return name + ": " + a + (data == null "" " (" + data + ")");
    }
  }

  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Tree Renderer Test");
    VSX2 parser = new VSX2();
    JTree tree = new JTree(parser.parse("testfile.xml"));

    // Steal the default icons from the default renderer...
    DefaultTreeCellRenderer rend1 = new DefaultTreeCellRenderer();
    IconAndTipRenderer rend2 = new IconAndTipRenderer(rend1.getOpenIcon(), rend1.getClosedIcon(),
        rend1.getLeafIcon());
    tree.setCellRenderer(rend2);
    ToolTipManager.sharedInstance().registerComponent(tree);

    frame.getContentPane().add(new JScrollPane(tree));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300400);
    frame.setVisible(true);
  }
}

interface IconAndTipCarrier {
  public Icon getIcon();

  public String getToolTipText();
}

class IconAndTipRenderer extends JLabel implements TreeCellRenderer {
  Color backColor = new Color(0xFF0xCC0xFF);

  Icon openIcon, closedIcon, leafIcon;

  String tipText = "";

  public IconAndTipRenderer(Icon open, Icon closed, Icon leaf) {
    openIcon = open;
    closedIcon = closed;
    leafIcon = leaf;
    setBackground(backColor);
    setForeground(Color.black);
  }

  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {
    setText(value.toString());
    if (selected) {
      setOpaque(true);
    else {
      setOpaque(false);
    }

    IconAndTipCarrier itc = null;
    if (value instanceof DefaultMutableTreeNode) {
      Object uo = ((DefaultMutableTreeNodevalue).getUserObject();
      if (uo instanceof IconAndTipCarrier) {
        itc = (IconAndTipCarrieruo;
      }
    else if (value instanceof IconAndTipCarrier) {
      itc = (IconAndTipCarriervalue;
    }
    if ((itc != null&& (itc.getIcon() != null)) {
      setIcon(itc.getIcon());
      tipText = itc.getToolTipText();
    else {
      tipText = " ";
      if (expanded) {
        setIcon(openIcon);
      else if (leaf) {
        setIcon(leafIcon);
      else {
        setIcon(closedIcon);
      }
    }
    return this;
  }

  public String getToolTipText() {
    return tipText;
  }
}

           
       
Related examples in the same category
1. A simple XML parser that builds a tree from SAX eventsA simple XML parser that builds a tree from SAX events
2. DOM Tree Walker Tree Model
3. Demo Tree WalkerDemo Tree Walker
4. XML Tree ViewXML Tree View
5. SAX Tree ViewerSAX Tree Viewer
w__w_w_.j__a_v_a_2_s_.__co_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.