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?
# 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
# 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?