Java XML Document Save to File saveDocument(String filename, byte[] document)

Here you can find the source of saveDocument(String filename, byte[] document)

Description

save Document

License

Apache License

Declaration

public static void saveDocument(String filename, byte[] document) throws Exception 

Method Source Code

//package com.java2s;
/*//from w  w  w.  j a v  a2s .co m
 * File: DataUtils.java
 * 
 * Copyright 2009 Muradora
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class Main {
    public static void saveDocument(String filename, byte[] document) throws Exception {
        Document doc = null;
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

            doc = docBuilder.parse(new ByteArrayInputStream(document));
        } catch (Exception e) {
            String message = "Unable to save file: " + filename;
            System.err.println(message);
            e.printStackTrace();
            throw new Exception(message, e);
        }

        saveDocument(filename, doc);
    }

    public static void saveDocument(String filename, Document doc) throws Exception {
        try {
            File file = new File(filename.trim());
            String data = format(doc);
            PrintWriter writer = new PrintWriter(file, "UTF-8");
            writer.print(data);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            String message = "Unable to save file: " + filename;
            System.err.println(message);
            e.printStackTrace();
            throw new Exception(message, e);
        }
    }

    public static String format(Document doc) throws Exception {
        OutputFormat format = new OutputFormat(doc);
        format.setEncoding("UTF-8");
        format.setIndenting(true);
        format.setIndent(2);
        format.setOmitXMLDeclaration(true);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Writer output = new OutputStreamWriter(out);

        XMLSerializer serializer = new XMLSerializer(output, format);
        serializer.serialize(doc);

        return new String(out.toByteArray(), "UTF-8");
    }

    public static String format(byte[] document) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        Document doc = docBuilder.parse(new ByteArrayInputStream(document));

        OutputFormat format = new OutputFormat(doc);
        format.setEncoding("UTF-8");
        format.setIndenting(true);
        format.setIndent(2);
        format.setOmitXMLDeclaration(true);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Writer output = new OutputStreamWriter(out);

        XMLSerializer serializer = new XMLSerializer(output, format);
        serializer.serialize(doc);

        return new String(out.toByteArray(), "UTF-8");
    }
}

Related

  1. saveDocument(Document doc, Writer w)
  2. saveDocument(Document document, File outputFile)
  3. saveDocument(final Document doc, String encoding, String docPath)
  4. saveDocument(final Document document, final File destination)
  5. saveDocument(final Document document, String filename)
  6. saveDocumentToFile(Document doc, File file)
  7. saveDocumentToFile(Document doc, String filename)
  8. saveDOM(Document doc, String filename)
  9. SaveDOM(Document document, String filename)