Java XML Node Serialize serialize(Node doc)

Here you can find the source of serialize(Node doc)

Description

serialize

License

Open Source License

Declaration

public static String serialize(Node doc) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
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.Node;

import com.google.common.collect.ImmutableMap;

public class Main {
    public static String serialize(Node doc) throws Exception {
        StringWriter outText = new StringWriter();
        serialize(new DOMSource(doc), outText);
        return outText.toString();
    }/* ww w. j  ava2 s.  co  m*/

    public static void serialize(Source source, Writer writer) throws Exception {
        serialize(source, writer, ImmutableMap.of(OutputKeys.ENCODING, "UTF-8", OutputKeys.INDENT, "yes"));
    }

    public static void serialize(Source source, Writer writer, Map<String, String> properties) throws Exception {
        StreamResult sr = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = null;
        t = tf.newTransformer();
        for (Map.Entry<String, String> me : properties.entrySet()) {
            t.setOutputProperty(me.getKey(), me.getValue());
        }
        t.transform(source, sr);
    }
}

Related

  1. getSerializer(final File file)
  2. serialise(Node node)
  3. serialiseBytes(Node n)
  4. serialize(Element e, boolean omitXMLDeclaration)
  5. serialize(Node n, StreamResult sr)
  6. serialize(Node node)
  7. serialize(Node node)
  8. serialize(Node node, File outdir, String fileName)