Java XML Node to String nodeToString(Node node)

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

Description

node To String

License

Open Source License

Declaration

public static String nodeToString(Node node) 

Method Source Code

//package com.java2s;
/*//  w  w  w  .  j  ava2  s  .c  om
 * #%L
 * Xacml4J Core Engine Implementation
 * %%
 * Copyright (C) 2009 - 2014 Xacml4J.org
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.io.StringWriter;

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.Element;
import org.w3c.dom.Node;

import com.google.common.base.Strings;

public class Main {
    private static TransformerFactory transformerFactory;

    public static String nodeToString(Node node) {
        if (node == null) {
            return null;
        }
        final Transformer transformer;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            throw new IllegalStateException(String.format(
                    "Failed to build %s", Transformer.class.getName()), e);
        }
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(new StringWriter());
        try {
            transformer.transform(source, result);
            return result.getWriter().toString();
        } catch (TransformerException e) {
            throw new IllegalArgumentException(
                    "Failed to serialize Node to String.", e);
        }
    }

    /**
     * Creates a {@link String} representation
     *
     * @param n a DOM node
     * @return a string representation
     */
    public static String toString(Element n) {
        if (n == null) {
            return null;
        }
        StringBuilder fqname = new StringBuilder();
        if (!Strings.isNullOrEmpty(n.getNamespaceURI())) {
            fqname.append('{').append(n.getNamespaceURI()).append('}');
        }
        fqname.append(n.getLocalName());
        return fqname.toString();
    }
}

Related

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