Java XML Node to String stringifyNode(Node node)

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

Description

stringify Node

License

Open Source License

Declaration

public static String stringifyNode(Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013,2014 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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
 *
 * Contributors:/*www .  j a  va2  s.  c o m*/
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.ByteArrayOutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String stringifyNode(Node node) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            Source source = new DOMSource(node);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Result result = new StreamResult(out);
            transformer.transform(source, result);
            return out.toString();

        } catch (TransformerConfigurationException e) {
            return null;
        } catch (TransformerFactoryConfigurationError e) {
            return null;
        } catch (TransformerException e) {
            return null;
        }
    }
}

Related

  1. nodeToString(Object node)
  2. nodeToString(org.w3c.dom.Node domNode)
  3. nodeToText(Node node)
  4. nodeToWriter(Node node, Writer writer)
  5. nodeToXmlString(Node node)
  6. toHtml(StringBuffer html, Node node)
  7. toString(final Node node)
  8. toString(final Node node)
  9. toString(final Node node)