Java XML Node to String elementToString(Node element)

Here you can find the source of elementToString(Node element)

Description

element To String

License

Open Source License

Declaration

public static final String elementToString(Node element)
            throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;

import java.io.StringWriter;

import javax.xml.transform.Transformer;
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 final String elementToString(Node element)
            throws TransformerFactoryConfigurationError, TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        return transform(transformer, element);
    }//from  w  w w .  ja  v  a 2 s.c o m

    private static final String transform(Transformer transformer, Node element) throws TransformerException {
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(element);
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        return xmlString;
    }
}

Related

  1. convertToString(Node node)
  2. convertToString(Node node)
  3. createXmlString(Node node)
  4. dump(Node node)
  5. dumpNode(Node node)
  6. getAttributeValue(Node node, String attrName)
  7. getDescriptionOfNode(Node node)
  8. getNodeAsInputStream(Node node)
  9. getNodeAsStream(Node node)