public void write(OutputStream out) throws Exception {
        XMLElement tglbXML = new XMLElement("TOUCHGRAPH_LB");
        tglbXML.setAttribute("version", "1.20");
        
        final XMLElement nodeSet = new XMLElement("NODESET");                
        tglbXML.addChild(nodeSet);
                
        TGForEachNode fen = new TGForEachNode() {
            public void forEachNode( Node node ) {
                LBNode lbNode = (LBNode) node;
                XMLElement nodeElt = new XMLElement("NODE");
                nodeElt.setAttribute("nodeID", lbNode.getID());
                
                XMLElement nodeLocation = new XMLElement("NODE_LOCATION");
                nodeLocation.setAttribute("x", ""+(int) lbNode.getLocation().getX());
                nodeLocation.setAttribute("y", ""+(int) lbNode.getLocation().getY());
                nodeLocation.setAttribute("visible", lbNode.isVisible() ? "true" : "false");
                nodeElt.addChild(nodeLocation);
                
                XMLElement nodeLabel = new XMLElement("NODE_LABEL");
                nodeLabel.setAttribute("label", lbNode.getLabel());
                nodeLabel.setAttribute("shape", ""+lbNode.getType());
                nodeLabel.setAttribute("backColor", encodeColor(lbNode.getBackColor()));
                nodeLabel.setAttribute("textColor", encodeColor(lbNode.getTextColor()));
                nodeLabel.setAttribute("fontSize", ""+lbNode.getFont().getSize());
                nodeElt.addChild(nodeLabel);
                
                XMLElement nodeURL = new XMLElement("NODE_URL");
                nodeURL.setAttribute("url", lbNode.getURL());
                nodeURL.setAttribute("urlIsLocal", lbNode.getURLIsLocal() ? "true" : "false");
                nodeURL.setAttribute("urlIsXML", lbNode.getURLIsXML() ? "true" : "false");
                nodeElt.addChild(nodeURL);
                
                XMLElement nodeHint = new XMLElement("NODE_HINT");
                nodeHint.setAttribute("hint", lbNode.getHint());
                nodeHint.setAttribute("width", ""+lbNode.getHintWidth());
                nodeHint.setAttribute("height", ""+lbNode.getHintHeight());
                nodeHint.setAttribute("isHTML", lbNode.getHintIsHTML() ? "true" : "false");
                nodeElt.addChild(nodeHint);
                                
                nodeSet.addChild(nodeElt);
            }
        };

        graphEltSet.forAllNodes(fen);
        
        final XMLElement edgeSet = new XMLElement("EDGESET");
        tglbXML.addChild(edgeSet);

        TGForEachEdge fee = new TGForEachEdge() {
            public void forEachEdge( Edge edge ) {
                LBEdge lbEdge = (LBEdge) edge;
                XMLElement edgeElt = new XMLElement("EDGE");                
                edgeElt.setAttribute("fromID", lbEdge.getFrom().getID());
                edgeElt.setAttribute("toID", lbEdge.getTo().getID());
                edgeElt.setAttribute("type", ""+lbEdge.getType());
                edgeElt.setAttribute("length", ""+(int) lbEdge.getLength());                              
                edgeElt.setAttribute("visible", lbEdge.isVisible() ? "true" : "false");
                edgeElt.setAttribute("color", encodeColor(lbEdge.getColor()));                
                edgeSet.addChild(edgeElt);
            }
        };

        graphEltSet.forAllEdges(fee);
        
        
        XMLElement parameters = new XMLElement("PARAMETERS");
        tglbXML.addChild(parameters);
        
        Enumeration paramNames = parameterHash.keys();
        while (paramNames.hasMoreElements()) {
            String name = (String) paramNames.nextElement();
            String value = (String) parameterHash.get(name);
            XMLElement param = new XMLElement("PARAM");
            param.setAttribute("name", name);
            param.setAttribute("value", value);
            parameters.addChild(param);
        }                  

        //InputStream prependDTD = getClass().getResourceAsStream("/TG_Prepend_DTD.xml");
        
        FileInputStream prependDTD = new FileInputStream(new File("RnaDVLib"+ File.separator + "TG.xml")); 
        /* Appends a mysterious empty string to the end to the TG_Prepend_DTD.xml
        int avail;
        while ((avail = prependDTD.available())!=0) {
            byte b[] = new byte[avail];
            prependDTD.read(b);
            out.write(b);
        }
        */
        
        int c; //Slow, but gets the job done for small files
        while((c = prependDTD.read())!=-1) {
            out.write(c);
        }
        
        prependDTD.close();
        
        XMLWriter writer                      
            = new XMLWriter(out);
        writer.write(tglbXML, true);
        out.close();
    }