[jdom-interest] How to: XMLOutputter to Buffer?
Ken Rune Helland
kenh at csc.no
Tue Mar 6 05:22:36 PST 2001
At 08:03 AM 3/6/2001 -0800, Kevin Baynes wrote:
>I'm using JDOM in a servlet/applet pair. In the servlet, I have a situation
>where I parse XML to JDOM and then write it to a string which I will
>eventually send to the applet. I do not use the servlet's PrintWriter until
>later, because I may want to change the string before sending it.
>
>I've been using XMLOutputter, which takes a Writer or an OutputStream. I've
>had to actually write out the contents to a temp file then read them back
>into my string... is there a way to do this without the temp file?
>
>Here's my current code:
>
> Element root = doc.getRootElement(); // root element is <groups>
>
> XMLOutputter xout = new XMLOutputter();
> File tempFile = new File("temp.dat");
> BufferedWriter outWriter = new BufferedWriter( new FileWriter(
>tempFile ) );
> xout.output( root, outWriter );
> outWriter.flush();
> outWriter.close();
> BufferedReader buff = new BufferedReader( new FileReader(
> tempFile ) );
> String aLine = null;
> while ( (aLine = buff.readLine()) != null ) {
> say(aLine.trim()); // this puts the data into a storage
> string
> }
> buff.close();
> tempFile.delete();
>
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
Hello
The java.io package has ByteArrayOutputStream to get a byte buffer
and CharArrayWriter and StringWriter to get a String or Char buffer.
You also have ByteArrayInputStream, CharArrayReader and StringReader
to turn these back into streams.
You have also the option of using a PipedWriter/PipredReader or
PipedOutPutStream/PipedInputStream pair to save memory by avoiding
buffering but then you will have to use two threads, one for the
writing to the pipe and one for the reading.
Best Regards
KenR
More information about the jdom-interest
mailing list