Java Utililty Methods XML Document Serialize

List of utility methods to do XML Document Serialize

Description

The list of methods to do XML Document Serialize are organized into topic(s).

Method

voidserialize(Document document, OutputStream out)
serialize
DOMImplementationLS ls = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = ls.createLSSerializer();
LSOutput output = ls.createLSOutput();
output.setByteStream(out);
serializer.write(document, output);
voidserialize(Document document, String fileName)
Serializes the XML document to a file.
if (document == null)
    return;
Exception targetException = null;
Transformer transformer;
try {
    transformer = TransformerFactory.newInstance().newTransformer();
    DOMSource source = new DOMSource(document);
    FileOutputStream os = new FileOutputStream(new File(fileName));
...
voidserialize(Document document, Writer writer)
serialize
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", new Integer(4));
try {
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(writer);
    transformer.transform(new DOMSource(document), result);
...
voidserialize(final Document doc, final OutputStream os, final String encoding)
Serializes a DOM.
if (doc == null)
    throw new IllegalArgumentException("No document provided.");
if (os == null)
    throw new IllegalArgumentException("No output stream provided");
if (encoding == null || encoding.isEmpty())
    throw new IllegalArgumentException("No encoding provided.");
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", Integer.valueOf(2));
...
voidserialize(final Document document, final OutputStream out)
Writes the DOM document to the given stream in pretty print format.
serialize(document, out, true);
StringserializeDocument(Document doc)
serialize a Document in a XML UTF8 String
ByteArrayOutputStream s = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult outputTarget = new StreamResult(s);
transformer.transform(source, outputTarget);
...
voidserializeDocument(Document doc, String filePath)
Serializes a Document to a file.
serializeObject(doc, filePath);
StringserializeDocument(Document document)
serialize Document
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
DOMSource domSource = new DOMSource(document);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.transform(domSource, streamResult);
...
voidserializeDocument(Document document, OutputStream os)
Serialize a DOM document to an output stream.
try {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(os);
    transformer.transform(source, result);
} catch (TransformerException e) {
...
byte[]serializeDocument(Document root)
serialize Document
Source source = new DOMSource((Node) root);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
return serializeXML(transformer, source);