Java XML Node Serialize serializeSource(Source source)

Here you can find the source of serializeSource(Source source)

Description

serialize Source

License

Open Source License

Declaration

public static String serializeSource(Source source) 

Method Source Code

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

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;

public class Main {
    public static String serializeSource(Source source) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {/*w ww  .  j av a  2  s.  c o  m*/
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();

            StreamResult dest = new StreamResult(baos);
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(source, dest);
        } catch (Exception e) {
            System.out.println("Error serializing node.  " + e.getMessage());
        }

        try {
            return baos.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            return baos.toString();
        }
    }
}

Related

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