Java XML Node to String asString(Node node)

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

Description

as String

License

Apache License

Declaration

public static String asString(Node node) 

Method Source Code


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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

public class Main {
    public static String asString(Node node) {
        StringWriter writer = new StringWriter();
        try {// www  . j a v a  2s .  c o m
            Transformer trans = TransformerFactory.newInstance().newTransformer();
            trans.setOutputProperty(OutputKeys.INDENT, "no");
            trans.setOutputProperty(OutputKeys.VERSION, "1.0");
            if (!(node instanceof Document)) {
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            }
            trans.transform(new DOMSource(node), new StreamResult(writer));
        } catch (final TransformerConfigurationException ex) {
            throw new IllegalStateException(ex);
        } catch (final TransformerException ex) {
            throw new IllegalArgumentException(ex);
        }
        return writer.toString();
    }
}

Related

  1. asString(Element elt)
  2. asString(Node node)
  3. asString(Node node)
  4. asString(XMLStreamReader xmlr)
  5. asXML(Node node)