code: public void serialize(DocumentImpl doc) throws IOException {
  		org.w3c.dom.Document w3cDoc = doc.getDocument();
  		if (w3cDoc instanceof HTMLDocumentImpl) {
  			// if w3cDoc has no document type default to html5: <!DOCTYPE html>
  			writer.write("<!DOCTYPE html");
  			DocumentType dt = w3cDoc.getDoctype();
  			if (dt != null) {
  				if (dt.getPublicId() != null) {
  					writer.write(" PUBLIC \"");
  					writer.write(dt.getPublicId());
  					writer.write("\"");
  				}
  				if (dt.getSystemId() != null) {
  					writer.write(" \"");
  					writer.write(dt.getSystemId());
  					writer.write("\"");
  				}
  			}
  			writer.write(">\r\n");
  		} else {
  			// if not html write xml declaration
  			writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
  		}
  
  		if (doc.getRoot() != null) {
  			write(((ElementImpl)doc.getRoot()).getNode());
  		}
  		writer.flush();
  	}
docs: Serialize document to the writer initialized by constructor. This method accept both XML and HTML documents and write
  prolog accordingly: HTML document type, respective XML declaration. After prolog write nodes tree recursively, invoking
  {@link #write(Node)} with document root.
  <p>
  When document nodes tree is complete flush the writer but does not close it.
  
  @param doc document to serialize.
  @throws IOException if write operation fails.