W3C home > Mailing lists > Public > public-wai-ert@w3.org > March 2008

How to serialize DOM in Java

From: Johannes Koch <johannes.koch@fit.fraunhofer.de>
Date: Wed, 05 Mar 2008 17:25:36 +0100
Message-ID: <47CEC980.7060301@fit.fraunhofer.de>
To: public-wai-ert@w3.org

Given a DOM Node

Node node ...

the following two ways can be used to serialize the Node into a String.

1. DOM3

DOMImplementationRegistry registry =
DOMImplementationLS impl =
    (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer lsSerializer = impl.createLSSerializer();
String s = lsSerializer.writeToString(Node node);


TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
StringWriter sw = new StringWriter();
serializer.transform(new DOMSource(node), new StreamResult(sw));
String s = sw.getBuffer().toString();

In order to create XMLContent RDF, you may have to do two separate 
serialization steps for the nodes to go into xmlLeadingMisc and xmlRest 
depending on whether there is a document type declaration.

I'll send another mail about the reparsing process.
Johannes Koch
BIKA Web Compliance Center - Fraunhofer FIT
Schloss Birlinghoven, D-53757 Sankt Augustin, Germany
Phone: +49-2241-142628    Fax: +49-2241-142065
Received on Wednesday, 5 March 2008 16:25:53 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:55:56 UTC