List of utility methods to do XML Document Serialize
String | serializeDOM(Document doc) serialize DOM DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); LSOutput output = domImplementation.createLSOutput(); output.setEncoding("UTF-8"); StringWriter stringOut = new StringWriter(); output.setCharacterStream(stringOut); lsSerializer.write(doc, output); return stringOut.toString(); ... |
byte[] | serializeToByteArray(Document doc) serialize To Byte Array TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); ... |
String | serializeToString(org.w3c.dom.Document doc) serialize To String try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.transform(domSource, result); ... |
String | serializeToStringLS(Document doc, Node node, String encoding) Serialize XML Node to string Note: this method is supposed to be faster than the Transform version but the output control is limited. DOMImplementationLS domImpl = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImpl.createLSSerializer();
LSOutput output = domImpl.createLSOutput();
output.setEncoding(encoding);
StringWriter writer = new StringWriter();
output.setCharacterStream(writer);
lsSerializer.write(node, output);
writer.flush();
...
|
void | serializeXML(Document doc, Writer writer, boolean addXmlDeclaration) serialize XML serializeXML(doc.getDocumentElement(), writer, addXmlDeclaration); |
String | serializeXmlObject(org.w3c.dom.Document docToSerialize) Serialize w3c dom document. org.w3c.dom.ls.DOMImplementationLS impl = (org.w3c.dom.ls.DOMImplementationLS) org.w3c.dom.bootstrap.DOMImplementationRegistry .newInstance().getDOMImplementation("LS"); org.w3c.dom.ls.LSSerializer writer = impl.createLSSerializer(); org.w3c.dom.DOMConfiguration config = writer.getDomConfig(); config.setParameter("xml-declaration", false); config.setParameter("format-pretty-print", Boolean.TRUE); String serializedXML = writer.writeToString(docToSerialize); return serializedXML; ... |
void | serializeXMLToFile(Document document, File output) Serializes an XML document to disk try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult result = new StreamResult(output); transformer.transform(domSource, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); ... |