protected Element parseCommentOrXMLDeclaration(Reader is,String endTag) throws IOException {
  char endTagChars[]=endTag.toCharArray();
  int endTagPos=0;
  StringBuilder text=new StringBuilder();
  boolean ended=false;
  while (!ended) {
    int in=read(is);
    if (in == -1) {
      break;
    }
    char c=(char)in;
    if (c == endTagChars[endTagPos]) {
      endTagPos++;
      if (endTagPos == endTagChars.length) {
        ended=true;
      }
    }
 else {
      if (endTagPos != 0) {
        if (c == endTagChars[0] && endTagPos > 1) {
          text.append(c);
          continue;
        }
        text.append(endTagChars,0,endTagPos);
        endTagPos=0;
      }
      text.append(c);
    }
  }
  String elementName=null;
  if (endTag.equals("-->")) {
    elementName="comment";
  }
 else   if (endTag.equals(">")) {
    elementName="XML declaration";
  }
 else {
    if (eventParser) {
      textElement(text.toString());
      return null;
    }
    return createNewTextElement(text.toString());
  }
  if (eventParser) {
    return null;
  }
  Element comment=createNewElement(elementName);
  comment.setAttribute("content",text.toString());
  comment.isComment=true;
  return comment;
}

protected Element parseCommentOrXMLDeclaration ( Reader is , String endTag ) throws IOException { char endTagChars [ ] = endTag . toCharArray ( ) ; int endTagPos = 0 ; StringBuilder text = new StringBuilder ( ) ; boolean ended = false ; while ( ! ended ) { int in = read ( is ) ; if ( in == - 1 ) { break ; } char c = ( char ) in ; if ( c == endTagChars [ endTagPos ] ) { endTagPos ++ ; if ( endTagPos == endTagChars . length ) { ended = true ; } } else { if ( endTagPos != 0 ) { if ( c == endTagChars [ 0 ] && endTagPos > 1 ) { text . append ( c ) ; continue ; } text . append ( endTagChars , 0 , endTagPos ) ; endTagPos = 0 ; } text . append ( c ) ; } } String elementName = null ; if ( endTag . equals ( "-->" ) ) { elementName = "comment" ; } else if ( endTag . equals ( ">" ) ) { elementName = "XML declaration" ; } else { if ( eventParser ) { textElement ( text . toString ( ) ) ; return null ; } return createNewTextElement ( text . toString ( ) ) ; } if ( eventParser ) { return null ; } Element comment = createNewElement ( elementName ) ; comment . setAttribute ( "content" , text . toString ( ) ) ; comment . isComment = true ; return comment ; }
this utility method is used to parse comments and xml declarations in the xml . the comment / declaration is returned as an element , but is flagged as a comment since both comments and xml declarations are not part of the xml dom . this method can be overridden to process specific xml declarations
