/**
   * Parse end of element from document. Collects character data to the end tag and returns it with
   * whitespace stripped. Throws an exception if a start tag is seen before an end tag, or if the
   * end tag seen does not match the expected name.
   *
   * @param tag element name expected
   * @return content text with whitespace stripped
   * @throws IOException if error reading document
   * @throws XmlPullParserException if expected element not found, or if other parse error
   */
  protected String parseEndTag(String tag) throws IOException, XmlPullParserException {
    m_buffer.setLength(0);
    while (true) {
      switch (m_parser.next()) {
        case XmlPullParser.CONTENT:
          m_buffer.append(m_parser.readContent());
          break;

        case XmlPullParser.END_TAG:
          m_parser.readEndTag(m_endTag);
          if (m_endTag.getLocalName().equals(tag)) {
            return m_buffer.toString().trim();
          }
          // fall through for error handling

        case XmlPullParser.START_TAG:
        case XmlPullParser.END_DOCUMENT:
          throw new XmlPullParserException("Missing expected end tag " + tag);
      }
    }
  }