Java XML Node to String nodeToString(Node node)

Here you can find the source of nodeToString(Node node)

Description

node To String

License

EUPL

Declaration

public static String nodeToString(Node node) 

Method Source Code

//package com.java2s;
/*/*from   w w w .j a va2s  .  co m*/
 * **************************************************-
 * ingrid-interface-csw
 * ==================================================
 * Copyright (C) 2014 - 2016 wemove digital solutions GmbH
 * ==================================================
 * Licensed under the EUPL, Version 1.1 or ? as soon they will be
 * approved by the European Commission - subsequent versions of the
 * EUPL (the "Licence");
 * 
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * 
 * http://ec.europa.eu/idabc/eupl5
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * **************************************************#
 */

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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 {
    private static ThreadLocal<Transformer> threadLocalTransformer = new ThreadLocal<Transformer>() {
        @Override
        public Transformer initialValue() {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            try {
                transformer = factory.newTransformer();
                transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
                return transformer;
            } catch (TransformerConfigurationException e) {
                new RuntimeException("Error creating Transformer", e);
            }
            return null;
        }
    };

    public static String nodeToString(Node node) {
        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            Transformer transformer = threadLocalTransformer.get();
            transformer.reset();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}

Related

  1. nodeToString(Node node)
  2. nodeToString(Node node)
  3. nodeToString(Node node)
  4. nodeToString(Node node)
  5. nodeToString(Node node)
  6. nodeToString(Node node)
  7. nodeToString(Node node)
  8. nodeToString(Node node)
  9. nodeToString(Node node)