The dom4j 1.6.1 HTMLWriter is missing an XML declaration even if set to XHTML mode. It is fixed by changing back to XMLWriter but then I'm missing all the HTML features.
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.HTMLWriter;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setXHTML(true);
format.setOmitEncoding(false);
format.setSuppressDeclaration(false);
HTMLWriter writer = null;
try {
writer = new HTMLWriter(format);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
writer.write(createDocument());
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Indeed it does. It's generally not a good idea to put an XML declaration at the top of an HTML file, and you should consider whether you really need it.
If you really do, then you'll need to create your own Writer class, extended from HTMLWriter, with an implementation of writeDeclaration(), either taken from the XMLWriter class or implemented to your requirements.