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 =
     DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
    (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer lsSerializer = impl.createLSSerializer();
String s = lsSerializer.writeToString(Node node);


2. JAXP

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 5 March 2008 16:25:54 GMT