Java Document to String
This is a very simple utility function to produce an indented XML document as a String given a Document. It throws a TransformerException if anything drastic goes wrong.
JAVA:
-
import java.io.*;
-
import org.w3c.dom.Document;
-
import javax.xml.transform.*;
-
import javax.xml.transform.dom.DOMSource;
-
-
...
-
-
TransformerFactory tFactory = TransformerFactory.newInstance();
-
Transformer transformer = tFactory.newTransformer();
-
-
DOMSource source = new DOMSource(document);
-
StreamResult result = new StreamResult(sw);
-
-
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
-
transformer.transform(source, result);
-
-
return sw.toString();
-
}
No Comments so far
Leave a comment
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>