Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; public class Main { public static String write(Document doc) throws Exception { return write(doc.getDocumentElement()); } public static String write(Element e) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); write(e, baos); return new String(baos.toByteArray(), "UTF-8"); } public static void write(Element root, OutputStream os) throws Exception { Writer writer = new OutputStreamWriter(os); write(root, writer); } /** * @param root * @param writer * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Element root, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Source input = new DOMSource(root); write(input, writer); } /** * @param reader * @param parser * @param output * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Reader reader, XMLReader parser, Result output) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { SAXSource input = new SAXSource(); InputSource inputSource = new InputSource(); inputSource.setCharacterStream(reader); input.setInputSource(inputSource); input.setXMLReader(parser); write(input, output); } /** * @param parser * @param writer * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Reader reader, XMLReader parser, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Result output = new StreamResult(writer); write(reader, parser, output); } public static void write(Source input, Result output) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { write(input, output, true); } public static void write(Source input, Result output, boolean indent) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { boolean omitxmldeclaration = true; Transformer idTransform = TransformerFactory.newInstance().newTransformer(); if (omitxmldeclaration) { idTransform.setOutputProperty("omit-xml-declaration", "yes"); } idTransform.setOutputProperty("encoding", "UTF-8"); if (indent) { idTransform.setOutputProperty("indent", "yes"); try { idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (Exception e) { // } } idTransform.transform(input, output); } /** * @param input * @param os * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Source input, Writer writer) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { Result output = new StreamResult(writer); write(input, output); } }