7 Replies Latest reply on Feb 2, 2007 4:25 PM by 843805


      Hi all

      I am trying to make an XMLViewer. Easiest way to do that I figured would be to grab an XML parser, parse the xml into a DOM Document, and convert the Document into a TreeModel.

      I found a lot of code for this on the internet. First, I downloaded the latest version of Xerces, the apache XML parser. Then I scanned the web some more and found the DOMTreeWalkerTreeModel code


      voila. I've got a treemodel.

      Now, my only problem is that I want the tree to look a little different.

      My XML looks like
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:a="http://www.provenir.com/schemas">
           <xsd:element name="a" type="b"/>
           <xsd:complexType name="c">
                     <xsd:element name="d" type="e" minOccurs="1" maxOccurs="1"/>
                <xsd:attribute name="Org" type="xsd:string"/>
                <xsd:attribute name="sessionID" type="xsd:string"/>
                <xsd:attribute name="stack" type="xsd:string"/>
                <xsd:attribute name="Version" type="xsd:string" use="optional"/>
                <xsd:attribute name="ProductType" type="xsd:string" use="optional"/>
      but when I install it in a JTree and view it I see branches like
      [xsd:schema: null]
        [xsd:element: null]
      I'd like to see the actual attributes in the nodes - the name and type fields at the very least.

      Anyone know how I can get the TreeWalker to display these nodes differently?
        • 1. Re: XMLViewer
          Bruce Eckel's Thinking in Java (free PDF download - search on Google) has complete example of showing XML as a tree.
          • 2. Re: XMLViewer
            Bruce Eckel's Thinking in Java (free PDF download -
            search on Google) has complete example of showing XML
            as a tree.
            On what page?

            Also, just showing XML doesn't actually solve my problem. I've got to get it into a TreeModel somehow
            • 3. Re: XMLViewer
              Once again - this book shows a complete example of showing XML as a tree. Download the free PDF and search for JTree inside.
              • 4. Re: XMLViewer
                Damn, i was sure that it was in that book. My apologies - last time i read it ws 5 years ago. Here is the link to Sun tutorial that shows a complete example:

                • 5. Re: XMLViewer
                  Also see


                  And many others from googling "jtree dom xml".
                  • 6. Re: XMLViewer
                    Few things:
                    1) Xerces is built-in to Java from 5.0 and up
                    2) Here's a TreeNode implementation that contains a org.w3c.dom.Node
                    3) There's an example of using it below the code

                    ------------ TreeNode ---------------
                    import java.util.*;
                    import javax.swing.tree.*;
                    import org.w3c.dom.*;
                    public class XMLTreeNode implements TreeNode {
                         private Node xmlNode;
                         private XMLTreeNode parent;
                         private ArrayList<XMLTreeNode> children;
                         private Boolean showAttributes;
                         public XMLTreeNode(Node xmlNode) {
                              this.xmlNode = xmlNode;
                              showAttributes = Boolean.TRUE;
                         protected XMLTreeNode(Node xmlNode, XMLTreeNode parent) {
                              this.parent = parent;
                         public boolean getShowAttributes() {
                              if (showAttributes != null) {
                                   return showAttributes.booleanValue();
                              if (parent != null) {
                                   return parent.getShowAttributes();
                              return false;
                         public void setShowAttributes(Boolean set) {
                              showAttributes = set;
                         public String getXMLTag() {
                              boolean includeAttributes = getShowAttributes();
                              if (xmlNode instanceof Element && includeAttributes) {
                                   Element e = (Element)xmlNode;
                                   StringBuilder buf = new StringBuilder();
                                   if (e.hasAttributes()) {
                                        NamedNodeMap attr = e.getAttributes();
                                        int count = attr.getLength();
                                        for (int i = 0; i < count; i++) {
                                             Node a = attr.item(i);
                                             if (i == 0) {
                                                  buf.append(" [");
                                             else {
                                                  buf.append(", ");
                                   return buf.toString();
                              else if (xmlNode instanceof Text) {
                                   return xmlNode.getNodeValue();
                              return xmlNode.getNodeName();
                         public List<XMLTreeNode> getChildren() {
                              if (children == null) {
                              return new ArrayList<XMLTreeNode>(children);
                         public Enumeration children() {
                              if (children == null) {
                              final Iterator<XMLTreeNode> iter = children.iterator();
                             return new Enumeration() {
                                  public boolean hasMoreElements() { return iter.hasNext(); }
                                  public Object nextElement() { return iter.next(); }
                         public boolean getAllowsChildren() {
                             return true;
                         public TreeNode getChildAt(int childIndex) {
                              if (children == null) {
                              return children.get(childIndex);
                         public int getChildCount() {
                              if (children == null) {
                              return children.size();
                         public int getIndex(TreeNode node) {
                              if (children == null) {
                              int i = 0;
                             for (XMLTreeNode c : children) {
                                  if (xmlNode == c.xmlNode) {
                                       return i;
                             return -1;
                         public TreeNode getParent() {
                             return parent;
                         public boolean isLeaf() {
                              if (xmlNode instanceof Element) {
                                   return false;
                              if (children == null) {
                             return children.isEmpty();
                         public String toString() {
                              return getXMLTag();
                         protected void loadChildren() {
                              NodeList cn = xmlNode.getChildNodes();
                              int count = cn.getLength();
                              children = new ArrayList<XMLTreeNode>(count);
                              for (int i = 0; i < count; i++) {
                                   Node c = cn.item(i);
                                   if (c instanceof Text && c.getNodeValue().trim().length() == 0) {
                                   children.add(new XMLTreeNode(cn.item(i), this));
                    ------------------ Example ------------------
                    import java.io.File;
                    import javax.swing.*;
                    import javax.swing.tree.DefaultTreeModel;
                    import javax.xml.parsers.DocumentBuilder;
                    import javax.xml.parsers.DocumentBuilderFactory;
                    import org.w3c.dom.DOMConfiguration;
                    import org.w3c.dom.Document;
                    public class XMLTreeModel {
                         public static void main(String[] args) throws Exception {
                              DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                              Document doc = db.parse(new File("test.xml"));
                              DOMConfiguration dc = doc.getDomConfig();
                              dc.setParameter("comments", Boolean.FALSE);
                              JTree tree = new JTree(new DefaultTreeModel(new XMLTreeNode(doc)));
                              JFrame f = new JFrame("Test");
                              f.add(new JScrollPane(tree));
                    • 7. Re: XMLViewer
                      Hi all

                      Just wanted to let you know that I got this sorted out. The solution was something like yours Jaspre. What I did was I created my own TreeCellRenderer to display the information I want