private void parseChildren(SafeXmlPullParser parser, D doc, E element) {
    boolean done = false;
    do {
      N child = null;
      parser.next();

      switch (parser.getCurrentType()) {
        case TEXT:
          child = parseText(parser, doc, element);
          break;
        case START_ELEMENT:
          child = parseElement(parser, doc, element);
          break;
        case END_ELEMENT:
          done = true;
          break;
      }
      if (child != null) {
        doc.insertBefore(element, child, null);
      }
      // This is a bit of judgment call. If this happens, the document is
      // invalid, since the closing tag is missing. By exiting the loop when
      // parser is out of tokens we're silently repairing the invalid doc.
    } while (!done && parser.hasNext());
  }