Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringWriter; 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.stream.StreamResult; import org.w3c.dom.Document; public class Main { public static String GetStringFromDoc(Document d) { /* DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(d); */ StringWriter output = new StringWriter(); try { //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output)); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output.toString(); } public static byte[] transform(Transformer transformer, Document useDocument) { byte[] returnData = new byte[0]; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(useDocument.getDocumentElement()); transformer.transform(domSource, new StreamResult(baos)); returnData = baos.toByteArray(); baos.close(); } catch (TransformerException te) { String err = new String(te.toString()); returnData = err.getBytes(); te.printStackTrace(); } catch (IOException ie) { String err = new String(ie.toString()); returnData = err.getBytes(); ie.printStackTrace(); } return returnData; } }