Java XML Node to String nodeToString(Node node)

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

Description

Get standard xml string of node

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

public static String nodeToString(Node node) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**// w w  w  . j a v  a2 s  .c  o  m
     * Get standard xml string of node
     * 
     * @param node
     * @return
     */
    public static String nodeToString(Node node) {
        StringWriter sw = new StringWriter();
        try {
            Transformer t = TransformerFactory.newInstance().newTransformer();
            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            t.setOutputProperty(OutputKeys.INDENT, "yes");
            t.transform(new DOMSource(node), new StreamResult(sw));
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

    /**
     * Get standard xml string of nodes
     * 
     * @param nl
     * @return
     */
    public static String nodeToString(NodeList nl) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            s.append(nodeToString(n));
        }
        return s.toString();
    }
}

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, boolean pretty)