code: public static String extractXML(Node node, int start, int length)
      {
          ExtractHandler handler = null;
          try {
              handler = new ExtractHandler(start, length);
              Transformer xformer = TransformerFactory.newInstance().newTransformer();
              xformer.transform(new DOMSource(node), new SAXResult(handler));
              return handler.getResult();
          } catch (Throwable t) {
              if (handler != null && handler.isFinished()) {
                  return handler.getResult();
              } else {
                  throw new RuntimeException("Failed to extract XML", t);
              }
          }
      }
docs: Extracts a well-formed XML fragment from the given DOM tree.
  
  @param node the root of the DOM tree where the extraction takes place
  @param start the index of the first character
  @param length the maximum number of characters in text nodes to include in the returned fragment
  @return a well-formed XML fragment starting at the given character index and having up to the specified length,
  summing only the characters in text nodes
  @since 1.6M2