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

StringtoString(Document doc)
Converts an XML document to a formatted XML string.
if (doc == null) {
    return "";
try {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
...
StringtoString(Document doc)
This method is used to convert a Document object into an XML String.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(doc);
try {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
    return new String(output.toByteArray(), "UTF-8");
} catch (Exception e) {
...
StringtoString(Document doc, String encoding, boolean indent)
to String
Source source = new DOMSource(doc);
StringWriter sw = new StringWriter();
Result result = new StreamResult(sw);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
if (indent) {
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
return sw.getBuffer().toString();
StringtoString(Document document)
to String
ByteArrayOutputStream out = null;
try {
    out = new ByteArrayOutputStream();
    defaultTextTransformer.transform(new DOMSource(document), new StreamResult(out));
    return new String(out.toByteArray(), "UTF-8");
} catch (TransformerConfigurationException e) {
    throw new RuntimeException(e);
} catch (TransformerException e) {
...
StringtoString(Document document)
to String
PrintStream out = null;
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
    out = new PrintStream(baos);
    print(out, document);
    return new String(baos.toByteArray(), "UTF-8");
} catch (Exception ex) {
} finally {
...
StringtoString(Document document)
to String
return toString((Node) document);
StringtoString(Document document)
to String
StringWriter writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(document), new StreamResult(writer));
...
StringtoString(Document document)
convert org.w3c.dom.Document to xml-String
Source source = new DOMSource(document);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
return stringWriter.getBuffer().toString();
StringtoString(final Document document)
Transform document to string representation.
final StringWriter stringWriter = new StringWriter();
final StreamResult streamResult = new StreamResult(stringWriter);
final Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, TRANSFORMER_YES);
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, TRANSFORMER_YES);
...
StringtoString(final Document document)
to String
try {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(document), new StreamResult(baos));
...