Java tutorial
/* * ***** BEGIN LICENSE BLOCK ***** * Zimbra Collaboration Suite Server * Copyright (C) 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software Foundation, * version 2 of the License. * * This program 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 General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. * If not, see <https://www.gnu.org/licenses/>. * ***** END LICENSE BLOCK ***** */ package com.zimbra.cs.dav; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class DomUtil { public static byte[] getBytes(Document doc) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); writeDocumentToStream(doc, baos); byte[] msg = baos.toByteArray(); return msg; } public static void writeDocumentToStream(Document doc, OutputStream out) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setTrimText(false); format.setOmitEncoding(false); XMLWriter writer = new XMLWriter(out, format); writer.write(doc); } }