[jdom-interest] XMLOutputter surpressDeclaration

Van Dooren, Damian VanDoorenD at cartierpartners.ca
Tue May 29 09:53:12 PDT 2001


When using XMLOutputter and setting the supressDeclaration it still prints a
newline. I doubt this is what is intended. I suggest it could be fixed one
of two ways. 

The first is within the output(Document doc, Writer out) method, we could
check to see if the declaration is surpressed: 

// Print new line after decl always, even if no other new lines
// Helps the output look better and is semantically
// inconsequential
if (!suppressDeclaration) {
    out.write(lineSeparator);
}


Or IMHO the better place for it, the printDeclaration(Document doc, Writer
out, String encoding) method. So remove it from output(Document doc, Writer
out) and add it to printDeclaration(Document doc, Writer out, String
encoding):

    /**
     * <p>
     * This will write the declaration to the given Writer.
     *   Assumes XML version 1.0 since we don't directly know.
     * </p>
     *
     * @param doc <code>Document</code> whose declaration to write.
     * @param out <code>Writer</code> to write to.
     * @param encoding The encoding to add to the declaration
     */
    protected void printDeclaration(Document doc,
                                    Writer out,
                                    String encoding) throws IOException {

        // Only print of declaration is not suppressed
        if (!suppressDeclaration) {
            // Assume 1.0 version
            if (encoding.equals("UTF8")) {
                out.write("<?xml version=\"1.0\"");
                if (!omitEncoding) {
                    out.write(" encoding=\"UTF-8\"");
                }
                out.write("?>");
            }
            else {
                out.write("<?xml version=\"1.0\"");
                if (!omitEncoding) {
                    out.write(" encoding=\"" + encoding + "\"");
                }
                out.write("?>");
            }
		// Print new line after decl always, even if no other new
lines
		// Helps the output look better and is semantically
		// inconsequential
		out.write(lineSeparator);
        }
    }    



-----
Damian Van Dooren
Information Technology
Cartier Partners Financial Group Inc.



More information about the jdom-interest mailing list