Java XML Document Serialize serializeXML(Document doc, Writer writer, boolean addXmlDeclaration)

Here you can find the source of serializeXML(Document doc, Writer writer, boolean addXmlDeclaration)

Description

serialize XML

License

Apache License

Declaration

public static void serializeXML(Document doc, Writer writer, boolean addXmlDeclaration) throws IOException 

Method Source Code


//package com.java2s;
/* ************************************************************************** *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.//from w  w  w. j  a v a  2  s. com
 *
 * This file is licensed to you 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.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.StringWriter;
import java.io.Writer;

import org.w3c.dom.Attr;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.w3c.dom.Text;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    /**
     * @param doc
     * @param out
     * @throws IOException
     */
    public static void serializeXML(Document doc, OutputStream out) throws IOException {
        serializeXML(doc.getDocumentElement(), out);
    }

    public static void serializeXML(Document doc, Writer writer) throws IOException {
        serializeXML(doc.getDocumentElement(), writer, true);
    }

    public static void serializeXML(Document doc, Writer writer, boolean addXmlDeclaration) throws IOException {
        serializeXML(doc.getDocumentElement(), writer, addXmlDeclaration);
    }

    public static void serializeXML(Element doc, OutputStream out) throws IOException {
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        serializeXML(doc, writer, true);
    }

    private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException {
        try {
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
            LSSerializer serializer = impl.createLSSerializer();
            DOMConfiguration config = serializer.getDomConfig();
            config.setParameter("xml-declaration", addXmlDeclaration);
            // config.setParameter("format-pretty-print", true);
            // config.setParameter("normalize-characters", true);
            LSOutput out = impl.createLSOutput();
            out.setCharacterStream(writer);
            serializer.write(doc, out);
        } catch (Throwable e) {
            throw new IOException(e.getMessage());
        }
    }

    public static String serializeXML(Node node) throws IOException {
        StringWriter writer = new StringWriter();
        if (!serializeXmlNode(node, writer, true)) {
            return null;
        }
        return writer.toString();
    }

    private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException {
        if (node == null) {
            return false;
        }
        short type = node.getNodeType();
        boolean result = true;
        switch (type) {
        case Node.ATTRIBUTE_NODE: {
            String text = ((Attr) node).getValue();
            writer.write(text);
            break;
        }
        case Node.TEXT_NODE: {
            String text = ((Text) node).getData();
            writer.write(text);
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = (Element) node;
            if (includeNode) {
                serializeXML(element, writer, false);
            } else {
                Node child = element.getFirstChild();
                while (child != null) {
                    serializeXmlNode(child, writer, true);
                    child = child.getNextSibling();
                }
            }
            break;
        }
        case Node.DOCUMENT_NODE: {
            Document doc = (Document) node;
            serializeXmlNode(doc.getDocumentElement(), writer, includeNode);
            break;
        }
        default:
            result = false;
            break;
        }
        return result;
    }
}

Related

  1. serializeDocument(Document root)
  2. serializeDOM(Document doc)
  3. serializeToByteArray(Document doc)
  4. serializeToString(org.w3c.dom.Document doc)
  5. serializeToStringLS(Document doc, Node node, String encoding)
  6. serializeXmlObject(org.w3c.dom.Document docToSerialize)
  7. serializeXMLToFile(Document document, File output)