Java XML Node to String asXmlString(Node node)

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

Description

as Xml String

License

Open Source License

Declaration

public static String asXmlString(Node node) throws IOException 

Method Source Code


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

import java.io.IOException;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 {
    public static String asXmlString(Node node) throws IOException {
        final Source source = new DOMSource(node);
        final StringWriter stringWriter = new StringWriter();
        final Result result = new StreamResult(stringWriter);
        final TransformerFactory factory = TransformerFactory.newInstance();

        try {/* w w  w  .j av  a 2  s .  c om*/
            final Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (TransformerException exc) {
            throw new IOException(exc.getMessage(), exc);
        }
    }
}

Related

  1. asString(Node node)
  2. asString(Node node)
  3. asString(Node node)
  4. asString(XMLStreamReader xmlr)
  5. asXML(Node node)
  6. convertToString(Node node)
  7. convertToString(Node node)
  8. createXmlString(Node node)
  9. dump(Node node)