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

Van Dooren, Damian VanDoorenD at icfg.com
Tue Mar 6 05:16:03 PST 2001


Use a ByteArrayOutputStream and pass that to xout.output().


> -----Original Message-----
> From: Kevin Baynes [mailto:kbaynes at renex.com]
> Sent: Tuesday, March 06, 2001 11:04 AM
> To: Jdom-Interest
> Subject: [jdom-interest] How to: XMLOutputter to Buffer?
> 
> 
> 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/yo
uraddr at yourhost.com



More information about the jdom-interest mailing list