code: public StringBuilder write(Document doc)
      {
          if (doc == null) return buffer;
          NormalOutput out = new NormalOutput(doc.getDocumentElement());
  
          // hint: this should be reimplemented !!! > use Writer in NormalOutput !
          ByteArrayOutputStream baos = new ByteArrayOutputStream(8 * 1024);
          out.dumpTo(new PrintStream(baos));
          try
          {
              buffer.append(baos.toString(Charset.defaultCharset().name()));
          } catch (UnsupportedEncodingException e)
          {
              e.printStackTrace();
          }
  
          return buffer;
      }
docs: Writes current representation of document. If empty output is produced,
  Document may be processed in parallel (and probably has not finished
  yet), therefore check if environment variable
  Constants.DOCUMENT_ASYNCHRONOUS_LOAD_PRIORITY_PROPERTY is set (value >=
  0). If so, wait until document is fully processed.
  
  @param doc
  the W3C Document
  @return the string builder with textual representation.
  
  @see Document
  @see StringBuilder
  @see Constants
  @see GeneralEventListener
  @see GeneralEvent