[jdom-interest] XMLOutputter newline and sockets

Steve Upton steveu at firebrandsoftware.com
Fri Aug 17 03:35:05 PDT 2001

Hi All, well probably more Jason Hunter and Damian Van Dooren actually  :)

Let me set the scene and then I'll explain why....

I'm currently looking at creating a client server app that uses XML for its
So, client opens socket sends some commands, gets some responses and then
socket is
closed. As a newbie to Sockets and XML I've relied heavily on examples to
learn this,
so there may be a better way, but the way to do this seems to be:

- Server suns and listens on socket
- Client runs and opens socket to server
- Server accespts connection and fires off new thread wich does readLine()
to get data
- Client sends command and then does readLine() to get response
- Process repeats until client is finished and terminates connection.

Ok, no problem so far, appart from the fact that what I'd like to do is :

Sender (could be client or server):
    Creates a JDOM and then uses outputter.build() to generate the XML

    String data = in.readLine()                      // Read complete XML
from input stream
    parser.build(new StringReader(data));    // Parse into JDOM

    ....                                                        // Walk tree
to extract data.

Unfortuantely this is not possible because even if you cannot easily
generate the XML in the first place
as a single line, unless you omit the version header, or run it through a
formatter to strip newlines before
sending it. This to me seemed odd, as there appeared to be a bug in the
XMLOutputter.build() method,
a newline was being added after the version string, even though the doc says
if no parameters are  passed to
the XMLOutputter constructor newlines are suppressed in the output.

However, thought I'd better take a look at the archives, as a newbie -
didn't want to ask a dumb question.
I then found the conversation between you guys on this very subject:


From: VanDoorenD at cartierpartners.ca (Van Dooren, Damian)
Date: Tue, 29 May 2001 12:53:12 -0400
Subject: [jdom-interest] XMLOutputter surpressDeclaration

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) {

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

     * <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\"");
            else {
                out.write("<?xml version=\"1.0\"");
                if (!omitEncoding) {
                    out.write(" encoding=\"" + encoding + "\"");
  // Print new line after decl always, even if no other new
  // Helps the output look better and is semantically
  // inconsequential

From: jhunter at collab.net (Jason Hunter)
Date: Tue, 29 May 2001 18:48:46 -0700
Subject: [jdom-interest] XMLOutputter surpressDeclaration

Fixed.  I choose door #2.  ;-)


Please, please can we have option #3 which is the same fix but with the
newline supressed
if setNewlines(false) .



Steve Upton
Firebrand Software

More information about the jdom-interest mailing list