Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class Main { public static final String ENCODING_START = "encoding=\""; public static final String PREAMBLE_START = "<?xml"; public static final String PREAMBLE_END = ">"; /** * Converts a Document to a String * * @param doc * The Document to be converted * @return The String representation of the Document */ public static String convertDocumentToString(final Document doc, final String encoding) { String retValue = convertDocumentToString(doc); /* * The encoding used is the encoding of the DOMString type, i.e. UTF-16 (http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-LSSerializer- * writeToString). However, we need to use UTF-8 (https://bugzilla.redhat.com/show_bug.cgi?id=735904). So do a simple text replacement. */ final String docEncoding = findEncoding(retValue); if (docEncoding != null) retValue = retValue.replace(docEncoding, encoding); return retValue; } /** * Convert an XML document to a string. * * @param doc * The Document to be converted * @param encoding * The encoding of the XML * @param entityDec * Any additional XML entity declarations * @return The String representation of the XML Document */ public static String convertDocumentToString(final Document doc, final String encoding, final String entityDec) { String retValue = convertDocumentToString(doc, encoding); final String docEncoding = findPreamble(retValue); if (docEncoding != null) retValue = retValue.replace(docEncoding, docEncoding + "\n" + entityDec); return retValue; } /** * Converts a Document to a String * * @param doc * The Document to be converted * @return The String representation of the Document */ public static String convertDocumentToString(final Document doc) { final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); final LSSerializer lsSerializer = domImplementation.createLSSerializer(); // lsSerializer.getDomConfig().setParameter("format-pretty-print", // Boolean.TRUE); final String xml = lsSerializer.writeToString(doc); return xml; } public static String findEncoding(final String xml) { final int encodingIndexStart = xml.indexOf(ENCODING_START); final int firstLineBreak = xml.indexOf("\n"); // make sure we found the encoding attribute if (encodingIndexStart != -1) { final int encodingIndexEnd = xml.indexOf("\"", encodingIndexStart + ENCODING_START.length()); // make sure the encoding attribute was found before the first // line break if (firstLineBreak == -1 || encodingIndexStart < firstLineBreak) { // make sure we found the end of the attribute if (encodingIndexEnd != -1) { return xml.substring(encodingIndexStart + ENCODING_START.length(), encodingIndexEnd); } } } return null; } public static String findPreamble(final String xml) { final int indexStart = xml.indexOf(PREAMBLE_START); // make sure we found the encoding attribute if (indexStart != -1) { final int indexEnd = xml.indexOf(PREAMBLE_END, indexStart + PREAMBLE_START.length()); // make sure we found the end of the attribute if (indexEnd != -1) { return xml.substring(indexStart, indexEnd + PREAMBLE_END.length()); } } return null; } }