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

Kevin Baynes kbaynes at renex.com
Tue Mar 6 08:03:45 PST 2001

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 );
	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

More information about the jdom-interest mailing list