/**
  * Parses an element.
  *
  * @param parser tokenizer
  * @param parentElement the parent element to attach the parsed node to
  * @return a new element.
  */
 private E parseElement(SafeXmlPullParser parser, D doc, E parentElement) {
   E element =
       doc.createElement(
           parser.getTagName(),
           CollectionUtils.newJavaMap(parser.getAttributes()),
           parentElement,
           null);
   parseChildren(parser, doc, element);
   return element;
 }
  /**
   * @param xmlString
   * @return parsed string
   */
  public D parse(String xmlString) {
    SafeXmlPullParser parser;
    try {
      parser = XmlParserFactory.buffered(xmlString);
    } catch (XmlParseException e) {
      throw new RuntimeException("Cannot parse xml: " + xmlString, e);
    }
    // TODO(ohler): This can be an infinite loop.  Fix that.
    while (parser.getCurrentType() != ItemType.START_ELEMENT) {
      parser.next();
    }

    D document =
        factory.create(parser.getTagName(), CollectionUtils.newJavaMap(parser.getAttributes()));
    parseChildren(parser, document, document.getDocumentElement());

    return document;
  }