protected String extractTextBetweenTags ( String stringContainingText , String tagname ) { String openTag = "<" + tagname . toUpperCase ( ) + ">" ; String closeTag = "</" + tagname . toUpperCase ( ) + ">" ; StringBuilder buf = new StringBuilder ( ) ; boolean record = false ; for ( int i = 0 ; i < stringContainingText . length ( ) - openTag . length ( ) ; i ++ ) { if ( stringContainingText . substring ( i , i + closeTag . length ( ) ) . equalsIgnoreCase ( closeTag ) ) { record = false ; break ; } if ( record ) { buf . append ( stringContainingText . charAt ( i ) ) ; } if ( stringContainingText . substring ( i , i + openTag . length ( ) ) . equalsIgnoreCase ( openTag ) ) { buf = new StringBuilder ( ) ; i += openTag . length ( ) - 1 ; record = true ; } } return buf . toString ( ) . trim ( ) ; }
Given some stringContainingText extracts the String between the tags