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

voidserialise(Document doc, OutputStream out)
serialise
TransformerFactory fac = TransformerFactory.newInstance();
Transformer tf = fac.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes"); 
Source input = new DOMSource(doc.getDocumentElement());
Result output = new StreamResult(out);
tf.transform(input, output);
voidserialiseDOM(Document doc, String filepath)
serialise DOM
Transformer transformer;
try {
    transformer = TransformerFactory.newInstance().newTransformer();
    Result output = new StreamResult(new File(filepath));
    Source input = new DOMSource(doc);
    transformer.transform(input, output);
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
...
voidserialize(Document d, File f)
serialize
try {
    doSerialize(d, f);
} catch (TransformerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
voidserialize(Document d, OutputStream out, URL transformerLocation, Map properties)
serialize
Source source = new DOMSource(d);
Transformer transformer = null;
if (transformerLocation != null) {
    Source stylesource = new StreamSource(transformerLocation.toString());
    transformer = tFactory.newTransformer(stylesource);
} else
    transformer = tFactory.newTransformer();
String documentEncoding = d.getXmlEncoding();
...
voidserialize(Document doc)
serialize
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource src = new DOMSource(doc);
StreamResult res = new StreamResult(System.out);
t.transform(src, res);
voidserialize(Document doc, Writer out)
serialize
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
...
Stringserialize(Document document, boolean prettyPrint)
serialize
Writer writer = new StringWriter();
serialize(document, prettyPrint, writer);
return writer.toString();
Stringserialize(Document document, boolean prettyPrint)
serialize
DOMImplementationLS impl = getDOMImpl();
LSSerializer serializer = impl.createLSSerializer();
DOMConfiguration config = serializer.getDomConfig();
if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
    config.setParameter("format-pretty-print", true);
config.setParameter("xml-declaration", true);
LSOutput output = impl.createLSOutput();
...
voidserialize(Document document, File targetFile)
serialize
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(new DOMSource(document), new StreamResult(targetFile));
} catch (TransformerException e) {
    throw new IOException(e);
voidserialize(Document document, OutputStream ostream)
serialize
Source domSource = new DOMSource(document);
try {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    try {
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
    } catch (IllegalArgumentException e) {
...