Java Utililty Methods XML Document to String

List of utility methods to do XML Document to String

Description

The list of methods to do XML Document to String are organized into topic(s).

Method

StringxmlDocumentToString(final Document document)
Generates a String that is populated with the textual representation of the XML DOM of the given Document.
final DOMImplementationLS domImplementation = (DOMImplementationLS) document.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
return lsSerializer.writeToString(document);
StringxmlDocumentToString(Node d)
Converts an XML document or node to an equivalent string.
StringWriter writer = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(d), new StreamResult(writer));
return writer.toString();
StringxmlDOMDocumentToString(Document doc)
Convert XML DOM document to a XML string representation
if (doc == null) {
    throw new RuntimeException("No XML DOM document (null)!");
StringWriter stringWriter = new StringWriter();
String strDoc = null;
try {
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
...
StringXMLDOMtoString(Document document)
XMLDO Mto String
String out = null;
try {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    Source source = new DOMSource(document);
    transformer.transform(source, result);
...
voidxmlToFile(Document doc, String fileNameToWrite)
xml To File
DOMSource domSource = new DOMSource(doc);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileNameToWrite)));
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, streamResult);
StringxmlToString(Document doc)
xml To String
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return result.getWriter().toString();
} catch (TransformerException ex) {
...
StringXMLtoString(Document doc)
XM Lto String
String output = null;
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    output = writer.getBuffer().toString().replaceAll("\n|\r", "");
...
StringxmlToString(Document doc)
Will get an XML Document as a String
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
StringxmlToString(Document document)
Convert XML Document to its string representation.
if (transformer == null) {
    throw new Exception("Transformer is null");
Source xmlSource = new DOMSource(document);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
transformer.transform(xmlSource, result);
return stringWriter.toString();
...
StringxmlToString(Document xml)
Converts a DOM Tree to a String.
try {
    TransformerFactory f = TransformerFactory.newInstance();
    StringWriter writer = new StringWriter();
    Transformer t = f.newTransformer();
    DOMSource src = new DOMSource(xml);
    StreamResult result = new StreamResult(writer);
    t.transform(src, result);
    return writer.toString();
...