Java XML Node Serialize serializeXML(Node e, Writer out)

Here you can find the source of serializeXML(Node e, Writer out)

Description

serialize XML

License

Open Source License

Declaration

public static void serializeXML(Node e, Writer out) throws Exception 

Method Source Code

//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);
    }/*from  w ww .  j a v  a  2s . c o  m*/

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

Related

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