Java XML Document to String documentToString(Document doc)

Here you can find the source of documentToString(Document doc)

Description

document To String

License

Open Source License

Declaration

private static String documentToString(Document doc) throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    private static String documentToString(Document doc) throws TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String xml = writer.getBuffer().toString().replaceAll("\n|\r", "");
        return xml;
    }//from ww w  .  j a v  a2  s . c o  m
}

Related

  1. document2String(Node document)
  2. document2XmlString(Document xmldoc)
  3. documentToString(Document d)
  4. documentToString(Document d)
  5. documentToString(Document doc)
  6. documentToString(Document doc)
  7. documentToString(Document doc)
  8. documentToString(Document doc)
  9. documentToString(Document doc)