Starting from JDK 9, LSSerializer skips the default namespace. The following test fails in JDK 9 and later:
public void testDefaultNS() throws Exception {
String xml = "<a "
+ "xmlns=\"http://openjdk_java_net/xml/defaultNS\" "
+ "xmlns:p1=\"http://openjdk_java_net/xml/serializer/\">"
+ "<b>in default namespace</b></a>";
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
LSSerializer ls = getSerializer(db);
String out = ls.writeToString(getDoc(db, xml));
Assert.assertTrue(out.contains("defaultNS"));
}
private LSSerializer getSerializer(DocumentBuilder db) throws Exception {
DOMImplementationLS di = (DOMImplementationLS) db.getDOMImplementation();
return di.createLSSerializer();
}
private Document getDoc(DocumentBuilder db, String xml) throws Exception {
InputSource is = new InputSource(new StringReader(xml));
return db.parse(is);
}