Java XML Node Serialize serializeToXml(Node node, OutputStream out)

Here you can find the source of serializeToXml(Node node, OutputStream out)

Description

serialize To Xml

License

Open Source License

Declaration

public static void serializeToXml(Node node, OutputStream out)
            throws TransformerException 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . jav a 2 s .  c  om*/
 * #%L
 * Xacml4J Core Engine Implementation
 * %%
 * Copyright (C) 2009 - 2014 Xacml4J.org
 * %%
 * This program 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 3 of the
 * License, or (at your option) any later version.
 * 
 * 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.io.OutputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
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 org.w3c.dom.Node;

import com.google.common.base.Preconditions;

public class Main {
    private static TransformerFactory transformerFactory;

    public static void serializeToXml(Node node, OutputStream out)
            throws TransformerException {
        serializeToXml(node, out, true, false);
    }

    public static void serializeToXml(Node node, OutputStream out,
            boolean omitXmlDecl, boolean prettyPrint)
            throws TransformerException {
        Preconditions.checkNotNull(node);
        Preconditions.checkNotNull(out);
        Preconditions.checkState(transformerFactory != null);
        Transformer t = transformerFactory.newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                omitXmlDecl ? "yes" : "no");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.INDENT, prettyPrint ? "yes" : "no");
        DOMSource source = new DOMSource(node);
        Result result = new StreamResult(out);
        t.transform(source, result);
    }
}

Related

  1. serializeNoNS(Node node)
  2. serializeRDFToHTML(String xmlRDF)
  3. serializeSource(Source source)
  4. serializeToString(Node node, String encoding)
  5. serializeToXML(Node node, boolean indent)
  6. serializeXML(Node e, Writer out)
  7. serializeXML(Node node)
  8. serializeXML(Node node)
  9. serializeXML(Node root)