[jdom-interest] How to: XMLOutputter to Buffer?

Kevin Baynes kbaynes at renex.com
Tue Mar 6 11:15:52 PST 2001


Beautiful. Thanks. I was able to turn that into this:

	Element root = doc.getRootElement(); // root element is <groups>
	XMLOutputter xout = new XMLOutputter();
	CharArrayWriter charw = new CharArrayWriter();
	xout.output( root, charw );
	say( new String( charw.toCharArray() ) );

-----Original Message-----
From: Ken Rune Helland [mailto:kenh at csc.no]
Sent: Tuesday, March 06, 2001 5:23 AM
To: Kevin Baynes; Jdom-Interest
Subject: Re: [jdom-interest] How to: XMLOutputter to Buffer?


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@yourho
st.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