Here you can find the source of xmlToString(Document doc)
Parameter | Description |
---|---|
doc | XML Document |
Parameter | Description |
---|
public static String xmlToString(Document doc) throws TransformerException
//package com.java2s; /**/*from w w w . j a va 2 s .c om*/ * Copyright 5AM Solutions Inc * Copyright SemanticBits LLC * Copyright AgileX Technologies, Inc * Copyright Ekagra Software Technologies Ltd * * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/cacis/LICENSE.txt for details. */ import org.w3c.dom.Document; 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 java.io.StringWriter; public class Main { /** * Will get an XML Document as a String * * @param doc XML Document * @return String xml as String * @throws javax.xml.transform.TransformerException exception */ public static String xmlToString(Document doc) throws TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); // initialize StreamResult with File object to save to file final StreamResult result = new StreamResult(new StringWriter()); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } }