[This local archive copy mirrored from the canonical site: http://www.docuverse.com/personal/saxdom.html; links may not have complete integrity, so use the canonical document at this URL if possible.]

SAXDOM


SAXDOM is an implementation of W3C Document Object Model (DOM) API using Simple API for XML (SAX).


What's New

04/06/98 - SAXDOM updated to support latest version of DOM spec (03/18/98)
02/11/98 - XML-Binary example added to the online demo. (Parse the Binary.xml file)
02/10/98 - Online demo is here.
02/09/98 - Swing example updated
02/09/98 - Version 0.3 Released. Minor bug fixes.

SAX will be updated soon so expect another update at that time.

Happy trails,

Don Park


Original Release Notes

SAXDOM builds W3C Document Object Model (DOM) using Simple API for XML (SAX). Since there are currently four XML parsers supporting SAX, XML developers now have four XML parsers capable of building DOM. Note that SAXDOM supports the Core of DOM Level 1 API only. XML extension to DOM which provides DTD information is not currently supported because SAX currently does not provide DTD information. SAX version 2 will support DTD information and SAXDOM will be extended to support the XML extension of DOM.

SAXDOM is currently fairly stable but since both SAX and DOM are moving targets so you can expect SAXDOM to gyrate along. I use SAXDOM in all of my XML projects so you can expect SAXDOM to be polished enough to be part of production quality software. SAX drivers I use are MSXML and AElfred.

Just in case I did not make it clear: I wrote SAXDOM for the general good of XML community and therefore, SAXDOM is in public domain and can be used for any commercial or non-commercial purpose. However, it will not cure common cold so please don't sell it over the counter as medicine <g>. I do intend to maintain it as long as I can or at least until DOM API is finalized.

Package Names:

W3C DOM specification does not say what its package name should be so I have used org.w3c.dom for the DOM API and org.w3c.dom.xml for XML specific DOM API.

For SAXDOM package itself, I have used org.xml.dom for now. I think it should probably be org.xml.dom.sax or org.xml.sax.dom since it is SAX related. I do appologize to Jon Bosak for using the package name, even temporarily, without his permission. I just didn't want to call it com.jstud.saxdom for obviously reasons ;-).


Changes in 04/06/98 Release

SAXDOM package now includes both source code and classes. It is also compressed so you will have to expand it or repackage it into a form more friendlier to the Java system you are using (jar, uncompressed ZIP, or just plain files).

SAXDOM package name has changed from org.xml.dom to org.xml.saxdom as of 04/06/98 release.

SaxDocumentParser class was removed in favor of SaxDOM.openDocument method. See the sample code below for more information. Since DOM API has changed quite a bit, you will have to make some changes anyway.

There is still no JavaDoc for DOM classes nor SAXDOM classes. Sorry about that but you will have to rely on the DOM specification for now.

org.w3c.dom.Element.normalize() method and SaxTreeIterator has not been implemented yet.

SaxDOM.openDocument is not part of the spec but something like it should be.


Source Code

SAXDOM source code is no longer distributed separatedly. You will find it in the distribution package along with the class files.

Please don't forget to send me your comments.

Don Park


Getting and Installing SAXDOM

You will need to get the SAX package first from here.

SAXDOM package containing SAXDOM files as well as W3C DOM files can be found here.

Once you have both packages, include both the SAX package and the SAXDOM package in the CLASSPATH.


Using SAXDOM

You will need to import SAXDOM package as well as W3C DOM package. For example:

import org.w3c.dom.*;
import org.xml.saxdom.*;

To read in a XML document, use SaxDOM class.

For example, when following code fragment is invoked from a method of JTree subclass, you will see a tree with the XML document as the root. Inside the root item, you will see zero or more PI and one document root element.

Document doc = SaxDOM.openDocument(url, SAX_DRIVER);
if (doc != null)
{
   Node xroot = doc;
   MutableTreeNode troot = new DefaultMutableTreeNode(xroot, true);
   addChildren(troot, xroot);
   DefaultTreeModel model = new DefaultTreeModel(troot);
   setModel(model);
}

Following is a typical Swing JTree method override for displaying XML elements in a tree:

    public String
convertValueToText (Object value, boolean selected,
    boolean expanded, boolean leaf, int row, boolean hasFocus)
{
    if (value instanceof DefaultMutableTreeNode)
    {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        Object userObject = node.getUserObject();
        if (userObject != null && userObject instanceof Node)
        {
            Node xnode = (Node)userObject;
            String text;
            switch (xnode.getNodeType())
            {
                case Node.DOCUMENT:
                    text = xnode.toString();
                    break;

                case Node.ELEMENT:
                    text = ((Element)xnode).getTagName();
                    if (xnode.hasChildNodes())
                    {
                        Node firstNode = xnode.getFirstChild();
                        if (firstNode.getNodeType() == Node.TEXT &&
                            firstNode.getNextSibling() == null &&
                            firstNode.toString().length() > 0)
                            text += " -- " + firstNode.toString();
		    }
		    break;

                case Node.ATTRIBUTE:
                    text = ((Attribute)xnode).getName();
                    break;
        
                case Node.PI:
                    PI pi = (PI)xnode;
                    text = pi.getName() + " -- " + pi.getData();
                    break;

                case Node.COMMENT:
                    text = ((Comment)xnode).getData();
                    break;

                case Node.TEXT:
                    text = ((Text)xnode).getData();
                    break;

                default:
                    text = xnode.toString();
                    break;
            }
            return text;
        }
    }
    return super.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
}

Following example methods shows how to create JTree nodes from DOM:

    protected void
addNode (MutableTreeNode parent, Node xnode)
{
    boolean include = false;
    switch (xnode.getNodeType())
    {
        case Node.DOCUMENT:
            include = true;
            break;

        case Node.ELEMENT:
            include = true;
            break;

        case Node.ATTRIBUTE:
            break;

        case Node.PI:
            include = true;
            break;

        case Node.COMMENT:
            break;

        case Node.TEXT:
            String rawdata = xnode.toString();
            String trimmed = rawdata.trim();
            if (rawdata.length() > trimmed.length())
                ((Text)xnode).setData(trimmed);
            if (xnode.getNextSibling() != null && trimmed.length() > 0)
                include = true;
            break;

        default:
            break;
    }

    if (include)
    {
        MutableTreeNode tnode =
            new DefaultMutableTreeNode(xnode, true);
        parent.insert(tnode, parent.getChildCount());

        addChildren(tnode, xnode);
    }
}

    protected void
addChildren (MutableTreeNode parent, Node node)
{
    if (node.hasChildNodes())
    {
        NodeEnumerator enum = node.getChildNodes().getEnumerator();
        for (Node child = enum.toFirst(); child != null; child = enum.toNext())
            addNode(parent, child);
    }
}

Don Park (donpark@quake.net)