Java XML Format printNode(Node node)

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

Description

print Node

License

Apache License

Declaration

protected static void printNode(Node node) 

Method Source Code


//package com.java2s;
//License from project: Apache 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;

public class Main {
    protected static void printNode(Node node) {
        try {/*from w  ww .  j a va2s .c om*/
            // Set up the output transformer
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            // Print the DOM node
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(node);
            transformer.transform(source, result);
            String xmlString = sw.toString();

            System.out.println(xmlString);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. print(Node node, String encoding)
  2. print(Node node, Writer out, boolean omitdecl)
  3. print(SOAPMessage msg, OutputStream out)
  4. printAttributeValue(Element node, String prefix)
  5. printNode(Node n)
  6. printNode(Node node, String fn)
  7. printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration)
  8. printNodeSubtreeXMLString(final Node node)
  9. printNodeToConsole(Node n, ByteArrayOutputStream byteArrayOS)