Run the following on JDK 5:
---%<---
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
public class TestDOMSerializerImpl {
public static void main(String[] args) throws Exception {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element el = doc.createElement("x");
DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
System.out.println(ls.createLSSerializer().writeToString(el));
}
}
---%<---
I get:
---%<---
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) Server VM (build 1.5.0_15-b04, mixed mode)
Exception in thread "main" org.w3c.dom.ls.LSException: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToString(DOMSerializerImpl.java:546)
at TestDOMSerializerImpl.main(TestDOMSerializerImpl.java:10)
---%<---
The lack of a proper stack trace is recorded in bug #6710741, but using a source debugger shows that the culprit is prepareForSerialization:
next = node.getNextSibling();
The bug appears to be fixed in JDK 6; perhaps the fix can be backported?