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

StringgetXML(Document doc)
transform the Document into a String
try {
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
...
StringgetXml(Document doc)
get Xml
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);
    String xmlString = result.getWriter().toString();
    return xmlString;
...
StringgetXML(Document pDocument)
returns an XML string.
String retString = null;
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.transform(new DOMSource(pDocument), new StreamResult(out));
    retString = out.toString();
    out.close();
...
byte[]toByteArray(final Document document)
To byte array.
if (document == null) {
    return null;
try {
    final String string = XML_HEADER_UTF8 + toString(document);
    return string.getBytes("utf-8");
} catch (final Exception e) {
    throw new RuntimeException(e);
...
byte[]toByteArray(final Document document)
to Byte Array
if (document == null) {
    throw new IllegalArgumentException("Document should not be null.");
final Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
ByteArrayOutputStream out = null;
try {
...
voidtoStream(Document document, OutputStream stream)
Saves an XML Document into a stream.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source input = new DOMSource(document);
Result output = new StreamResult(stream);
transformer.transform(input, output);
StringtoString(Document d)
to String
DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
return lsSerializer.writeToString(d);
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)
print document to string
Transformer tf = tff.newTransformer();
StringWriter writer = new StringWriter();
tf.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString();
return output;
StringtoString(Document doc)
Transform a XML Document to String
try {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
...