[jdom-interest] XMLOutputter newline and sockets
Jason Hunter
jhunter at acm.org
Fri Aug 17 11:50:18 PDT 2001
The default is for newlines not to be added, but the problem is SAX
doesn't report newlines outside the root element so that results in
output like:
<?xml version="1.0"><!-- comment --><root>
<child/>
</root>
And people don't like that. They say the file looked like this before
and I want it to stay that way:
<?xml version="1.0">
<!-- comment -->
<root>
<child/>
</root>
So we add new lines outside the root element. I agree it's less than
ideal, but with a few overrides you can get whatever you want. What's
really needed is some tunable output feature that lets you specify which
general output style you want:
one line
best effort to round trip
best effort to beautify
using two spaces, four spaces, tabs, etc
-jh-
Steve Upton wrote:
>
> 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
> communication.
> 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
>
> Receiver:
> 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) {
> 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);
> }
> }
>
> ------------------------------------------------------------------------
> 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. ;-)
>
> -jh-
> ------------------------------------------------------------------------
>
> Please, please can we have option #3 which is the same fix but with the
> newline supressed
> if setNewlines(false) .
>
> Thanks
>
> Steve
>
> _____________________________________________________________
> Steve Upton
> Firebrand Software
> http://www.firebrandsoftware.com
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
More information about the jdom-interest
mailing list