Hi Thomas

This is a fairly common gotcha people have been hitting so I've patched the
code to fix it once and for all.

Using the version of the code you have, the quick fix is to add a call to
flush() as follows:-

> class ParseTest {
>      static public void main (String args[]) {
>          try {
>              if (args.length == 0) {
>                  System.out.println ("Error: must specify XML file.");
>              }
>              else {
>                  SAXReader reader = new SAXReader (false);    // No
> validation.
>                  Document doc = reader.read (args[0]);
>                  XMLWriter writer = new XMLWriter(System.out,
> OutputFormat.createPrettyPrint());
>                  writer.write (doc);

                    writer.flush();

>                  // System.out.println (doc.asXML());
>              }
>          }
>          catch (Exception e) {
>              System.out.println ("Exception: " + e.getMessage());
>          }
>      }
> }
>

The longer explanation is that the XMLWriter internally uses a Writer
instance when outputting text. If you create an XMLWriter from an
OutputStream (such as System.out) then internally it creates a
BufferedWriter containing a OuputStreamWriter.

I've patched the code such that when an OutputStream is used, the XMLWriter
knows it should call flush() on the BufferedWriter to force any output
remaining through to the underlying OutputStream.

So using the current CVS image, this problem should finally go away.

(Toby, we might want to change your cookbook to remove the recommendation
that people call flush() after writing a Document).


James

----- Original Message -----
From: "Thomas Nichols" <[EMAIL PROTECTED]>
To: "dom4j-dev" <[EMAIL PROTECTED]>
Sent: Sunday, June 17, 2001 12:57 AM
Subject: [dom4j-dev] More output questions


> Good day,
> I'm having some peculiar problems with large files.  I do the following:
>
>            XMLWriter writer = new XMLWriter(System.out,
> OutputFormat.createPrettyPrint());
>
>
> For small files, all works fine. For the biggest, I get errors -- the
> output is abruptly truncated, in the middle of an element: as if someone
> has taken a pair of scissors and chopped the listing in half. For a given
> document, this happens always in the same place, and is unaffected by heap
> settings (-Xmx150M has the same result as -Xmx 16M).
>
> Replacing System.out with
> new BufferedOutputStream (new FileOutputStream ("ParseTest.out"))
> has no effect - the file is truncated in exactly the same place.
>
>
> FWIW, at startup I get the following message printed to stdout:
> Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred
> instead
>
> Bizarrely, System.out.println (doc.asXML()) works fine, except for CRLF/LF
> issues.
>
> How can I track this truncation problem down - any suggestions?
>
> System: dom4j-coming-soon-0.5 daily snapshot grabbed on 2001-06-15
> Mandrake Linux 7.2, Sun JDK 1.3.0_02, 256Mb RAM, AMD Duron 800 (etc
etc...)
> CLASSPATH:
>
/home/thomasn/src/dx2/classes:/usr/local/dom4j-coming-soon-0.5/dom4j-full.ja
r:/usr/local/jdom-b6/build/jdom.jar:/usr/local/jdom-b6/lib/xerces.jar:/usr/l
ocal/jdom-b6:/usr/local/jakarta-log4j-1.0.4/classes:/home/thomasn/src:/usr/l
ocal/enhydra4.0/lib/servlet.jar
>
> Any suggestions would be very welcome.
> Thanks,
> Thomas.
>
>
> ==== ParseTest.java ====
>
>
> package test;
>
> import org.dom4j.io.*;
> import org.dom4j.*;
> import org.dom4j.tree.*;
>
> class ParseTest {
>      static public void main (String args[]) {
>          try {
>              if (args.length == 0) {
>                  System.out.println ("Error: must specify XML file.");
>              }
>              else {
>                  SAXReader reader = new SAXReader (false);    // No
> validation.
>                  Document doc = reader.read (args[0]);
>                  XMLWriter writer = new XMLWriter(System.out,
> OutputFormat.createPrettyPrint());
>                  writer.write (doc);
>                  // System.out.println (doc.asXML());
>              }
>          }
>          catch (Exception e) {
>              System.out.println ("Exception: " + e.getMessage());
>          }
>      }
> }
>
>
> _______________________________________________
> dom4j-dev mailing list
> [EMAIL PROTECTED]
> http://lists.sourceforge.net/lists/listinfo/dom4j-dev
>



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to