Java XML Node to String toText(Node node)

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

Description

to Text

License

BSD License

Declaration

public static String toText(Node node) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.io.ByteArrayOutputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
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 {
    public static String toText(Node node) {
        try {//  w  ww .  ja  v  a2s  .  c o m
            DOMSource source = new DOMSource(node);
            Transformer identity = TransformerFactory.newInstance().newTransformer();
            identity.setOutputProperty(OutputKeys.INDENT, "no");
            identity.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            // identity.setOutputProperty(OutputKeys.METHOD, "text");
            // identity.setOutputProperty(OutputKeys.METHOD, "xml");
            // identity.setOutputProperty(OutputKeys.METHOD, "html");
            // class
            // com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Result result = new StreamResult(baos);
            identity.transform(source, result);
            return new String(baos.toString("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. toString(Node node, StringBuilder sb)
  2. toString(Node xml)
  3. toString(NodeList nodes)
  4. toStringE(Node element)
  5. toText(Node node)
  6. toWellKnowName(Node vertexStyleNode)
  7. toWriter(Node node, Writer writer, Map outputProperties)
  8. toXML(Node node)
  9. toXml(Node node)