DOM - Creating Documents

Kay Michael Michael.Kay at icl.com
Thu Apr 22 17:49:22 BST 1999


Ronald Bourret asked how do you create a Document using various products.
For the record, here are the relevant drivers from SAXON:

Datachannel:

    public Document build (InputSource source)
        throws java.io.IOException, org.xml.sax.SAXException
    {
        com.datachannel.xml.om.Document doc = new
com.datachannel.xml.om.Document();
        try {
            if (null != source.getByteStream()) {
                
                // byte stream supplied
                
                doc.loadFromInputStream(source.getByteStream());
                
            } else if (null != source.getCharacterStream()) {
                
                // character stream supplied [horrible code and not tested!]
                
                Reader r = source.getCharacterStream();
                StringBuffer sb = new StringBuffer(10000);
                char[] cbuf = new char[10000];
                int bytes = 0;
                while (true) {
                    bytes = r.read(cbuf);
                    if (bytes<0) break;     // end of file
                    sb.append(cbuf, 0, bytes);
                }
                doc.loadXML(sb.toString());
                
            } else {

                // URL supplied
                
                doc.load(source.getSystemId());
            }
        }
        catch (Exception e) {
            throw new SAXException(e);
        }

        return doc;
    }

Docuverse:

    public Document build(InputSource source)
    {
        DOM dom = new DOM();
        dom.setReader(new SAXONFreedomDriver());        
        return dom.openDocument(source);
    }

    // inner class

    /**
    *
    * SAXONFreedomDriver<BR>
    * Subclasses DOM-SDK's SAXReader class to use a supplied parser<BR>
    * This class is used to interface SAXON with Docuverse
    * and is of no direct concern to applications.
    *
    */

    private class SAXONFreedomDriver extends
com.docuverse.dom.util.SAXReader
    {
        protected Parser createParser (com.docuverse.dom.DOM dom)
        {
            return givenparser;
        }
    }

IBM:

    public Document build (InputSource source)
        throws java.io.IOException, org.xml.sax.SAXException
    {
        Document doc = null;
        com.ibm.xml.parser.Parser p;
        try {
            p = new com.ibm.xml.parser.Parser(source.getSystemId());
            if (null != source.getByteStream()) {
                doc = p.readStream(source.getByteStream());
            } else if (null != source.getCharacterStream()) {
                doc = p.readStream(source.getCharacterStream());
            } else {
                doc = p.readStream(
                        p.getInputStream(
                            source.getSystemId(),
                            null,
                            source.getSystemId()));
            }
        } catch (Exception e) {
            p = null;
            throw new SAXException(e);
        }
        return doc;
    }

ORACLE:

    public Document build(InputSource source)
            throws java.io.IOException, org.xml.sax.SAXException
    {
        oracle.xml.parser.XMLParser p = new oracle.xml.parser.XMLParser();
        p.parse(source);
        return p.getDocument();
    }

SUN:

    public Document build(InputSource source)
        throws java.io.IOException, org.xml.sax.SAXException
    {
        XmlDocumentBuilder b = new XmlDocumentBuilder();
        b.setDisableNamespaces(true);
        givenparser.setDocumentHandler(b);
        givenparser.parse(source);
        return b.getDocument();
    }


Interesting if nothing else for the sheer variety of different ways of
achieving the same thing! Note some of these allow you to select your parser
first, others don't.

Mike Kay

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)




More information about the Xml-dev mailing list