Java XML Node Serialize serializeToString(Node node, String encoding)

Here you can find the source of serializeToString(Node node, String encoding)

Description

Serialize XML Document to string using Transformer

License

Open Source License

Parameter

Parameter Description
doc XML Document

Exception

Parameter Description
IOException an exception

Return

String representation of the the Document

Declaration

public static String serializeToString(Node node, String encoding) throws IOException 

Method Source Code

//package com.java2s;
/**//w ww .j a va2  s  .  c o  m
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * StringUtils.java
 *
 * Who   Date       Description
 * ====  =========  =====================================================
 * WY    09Apr2015  Added null check to findAttribute()
 * WY    03Mar2015  Added serializeToString() and serializeToByteArray()
 * WY    27Feb2015  Added findAttribute() and removeAttribute()
 * WY    23Jan2015  Initial creation - moved XML related methods to here
 */

import java.io.IOException;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.Document;

import org.w3c.dom.Node;

public class Main {
    public static String serializeToString(Document doc) throws IOException {
        String encoding = doc.getInputEncoding();
        if (encoding == null)
            encoding = "UTF-8";

        return serializeToString(doc, encoding);
    }

    /**
     * Serialize XML Document to string using Transformer
     * 
     * @param doc XML Document
     * @return String representation of the the Document
     * @throws IOException
     */
    public static String serializeToString(Node node, String encoding) throws IOException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = tFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            throw new IOException("Unable to serialize XML document");
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        DOMSource source = new DOMSource(node);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            throw new IOException("Unable to serialize XML document");
        }
        writer.flush();

        return writer.toString();
    }
}

Related

  1. serializeNode(Node node, String indentLevel, StringWriter writer)
  2. serializeNode(Node xmlNode)
  3. serializeNoNS(Node node)
  4. serializeRDFToHTML(String xmlRDF)
  5. serializeSource(Source source)
  6. serializeToXML(Node node, boolean indent)
  7. serializeToXml(Node node, OutputStream out)
  8. serializeXML(Node e, Writer out)
  9. serializeXML(Node node)