Description
Writes a document as a string.
License
Open Source License
Parameter
Parameter | Description |
---|
doc | the document |
omitXmlDeclaration | true to omit the XML declaration |
Exception
Parameter | Description |
---|
Exception | an exception |
Return
the written document, or null if the conversion failed
Declaration
public static String writeDocument(Document doc, boolean omitXmlDeclaration) throws Exception
Method Source Code
//package com.java2s;
/****************************************************************************
*
* Copyright (c) 2005-2012, Linagora/* w w w .j av a 2 s .co m*/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
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 {
/**
* Writes a document as a string.
* @param doc the document
* @param omitXmlDeclaration true to omit the XML declaration
* @return the written document, or null if the conversion failed
* @throws Exception
*/
public static String writeDocument(Document doc, boolean omitXmlDeclaration) throws Exception {
String result = null;
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
if (omitXmlDeclaration)
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(domSource, streamResult);
result = writer.toString();
return result;
}
}
Related
- write(Document doc, Result result)
- write(String filename, Document document, boolean addDocType)
- write2xml(Document document)
- write2Xml(Document document)
- write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
- writeDocument(Document document)
- writeDocumentToStreamResult(Document document, StreamResult outputTarget)
- writeToBytes(Document document)
- writeXML(Document d)