Probably Simple XML toString

I've been trying to convert an xml document to a string, but I'm having a problem converting. I'm reading the XML from a file.

Document document = builder.parse(new File(filename));

And I'm sure that works. I can parse the result. But then I try to return what I've read using:

return (document.toString());

Every attempt I've made at using toString() has failed; even when I just try to write it out. What's the trick?

[457 byte] By [RogerFGay1a] at [2007-11-27 11:43:48]
# 1

I don't think that is right way of doing it, try it out this way:

Document document;

TransformerFactory tFactory =

TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);

StringWriter sw=new StringWriter();

StreamResult result = new StreamResult(sw);

transformer.transform(source, result);

String xmlString=sw.toString();

Message was edited by:

java_queen

java_queena at 2007-7-29 17:52:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Im sure this is not the case but Ill ask anyway:

Your post indicates youre returning the document from a method. Are you simply reading the document and returning it as a String? If so, why not skip all the complexity and overhead related with factories, builders, etc and simply read the file as you would any other text document?

Super_Squirrela at 2007-7-29 17:52:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...