Java Utililty Methods XML Node Serialize

List of utility methods to do XML Node Serialize

Description

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

Method

ContentHandlergetSerializer(final File file)
Get a serializer to write XML to a file.
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
final Transformer transformer = transformerHandler.getTransformer();
final Properties format = new Properties();
format.put(OutputKeys.METHOD, "xml");
format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
format.put(OutputKeys.ENCODING, "UTF-8");
format.put(OutputKeys.INDENT, "yes");
...
Stringserialise(Node node)
Serialises the XML node into a string.
try {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer serializer = lsImpl.createLSSerializer();
    return serializer.writeToString(node);
} catch (Exception e) {
    log.fine("could not serialise XML node: " + e);
    return "";
...
byte[]serialiseBytes(Node n)
serialise Bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
serialise(n, bos);
return bos.toByteArray();
Stringserialize(Element e, boolean omitXMLDeclaration)
Serialize an XML Element into a String.
if (e != null) {
    try {
        DOMSource domSource = new DOMSource(e);
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                ((omitXMLDeclaration) ? "yes" : "no"));
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
...
Stringserialize(Node doc)
serialize
StringWriter outText = new StringWriter();
serialize(new DOMSource(doc), outText);
return outText.toString();
voidserialize(Node n, StreamResult sr)
serialize
TransformerHandler hd = newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(n);
serializer.transform(source, sr);
Stringserialize(Node node)
serialize
StringWriter writer = new StringWriter();
try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
} catch (javax.xml.transform.TransformerException caught) {
    throw new Error(caught);
return writer.toString();
Stringserialize(Node node)
serialize
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(node);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);
    transformer.transform(source, result);
...
voidserialize(Node node, File outdir, String fileName)
Serialize the supplied DOM node to the specified file in the specified output directory.
serialize(node, new StreamResult(new File(outdir, fileName)));
Stringserialize(NodeList nodeList)
Serialise the supplied W3C DOM subtree.
return serialize(nodeList, false);