Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringWriter;

import java.io.Writer;

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

public class Main {
    public static void serializeXML(Node e, Writer out) throws Exception {
        DOMSource domSource = new DOMSource(e);
        StreamResult streamResult = new StreamResult(out);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        // turn off <?xml...?> stuff as for documents that were parsed with
        // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which
        // it should not, since we always serialize as UTF-8
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        // serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    }

    public static String serializeXML(Node e) throws Exception {
        StringWriter result = new StringWriter();
        serializeXML(e, result);
        return result.toString();
    }
}