Here you can find the source of xmlDOMDocumentToString(Document doc)
Parameter | Description |
---|---|
doc | XML DOM document |
Parameter | Description |
---|---|
Exception | in error case |
public static String xmlDOMDocumentToString(Document doc) throws Exception
//package com.java2s; //License from project: Apache License import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; 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 { /**// w w w .j av a 2 s . c o m * Convert XML DOM document to a XML string representation * * @param doc * XML DOM document * @return XML string * @throws Exception * in error case */ public static String xmlDOMDocumentToString(Document doc) throws Exception { 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(); // transformerFactory.setAttribute("nIndent-number", new Integer(4)); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult); stringWriter.flush(); strDoc = stringWriter.toString(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (stringWriter != null) { stringWriter.close(); stringWriter = null; } } return strDoc; } }