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

StringdocumentToString(Node document)
document To String
try {
    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
Stringdom2String2(Document document)
dom String
DOMImplementationRegistry registry;
try {
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer ser = domImplLS.createLSSerializer(); 
    LSOutput out = domImplLS.createLSOutput();
    StringWriter stringOut = new StringWriter(); 
    out.setCharacterStream(stringOut);
...
voidDOMaXML(Document doc, String nome)
DO Ma XML
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    StreamResult sr = new StreamResult(new File(nome));
    DOMSource doms = new DOMSource(doc);
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(doms, sr);
} catch (Exception e) {
...
StringBufferdomToString(Document document)
dom To String
if (document == null) {
    return new StringBuffer();
return transformToString(new DOMSource(document), null);
StringdomToString(final Document document)
dom To String
String result = null;
StringWriter strWtr = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
if (document != null) {
    StreamResult strResult = new StreamResult(strWtr);
    transformer.transform(new DOMSource(document.getDocumentElement()), strResult);
    result = strResult.getWriter().toString();
...
StringgetDocumentAsString(Document doc)
get Document As String
StringWriter sw = new StringWriter();
tf.newTransformer().transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
StringgetDocumentAsString(Document doc)
get Document As String
StringWriter writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.toString();
StringgetDocumentAsString(Document document)
get Document As String
Source source = new DOMSource(document);
StringWriter sw = new StringWriter();
Result streamResult = new StreamResult(sw);
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.transform(source, streamResult);
...
StringgetDocumentAsString(Node node, boolean prettyXml)
get Document As String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
if (prettyXml) {
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(node), new StreamResult(baos));
return baos.toString("UTF-8");
...
StringgetDocumentAsString(Resource resource)
get Document As String
Document document = getDocument(resource);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
return output;
...