public final static int readXMLUntil(final StringBuilder out, final String in, final int start, final char... end)
    {
        int pos = start;
        boolean inString = false;
        char stringChar = 0;
        while (pos < in.length())
        {
            final char ch = in.charAt(pos);
            if (inString)
            {
                if (ch == '\\')
                {
                    out.append(ch);
                    pos++;
                    if (pos < in.length())
                    {
                        out.append(ch);
                        pos++;
                    }
                    continue;
                }
                if (ch == stringChar)
                {
                    inString = false;
                    out.append(ch);
                    pos++;
                    continue;
                }
            }
            switch (ch)
            {
            case '"':
            case '\'':
                inString = true;
                stringChar = ch;
                break;
            }
            if (!inString)
            {
                boolean endReached = false;
                for (int n = 0; n < end.length; n++)
                {
                    if (ch == end[n])
                    {
                        endReached = true;
                        break;
                    }
                }
                if (endReached)
                {
                    break;
                }
            }
            out.append(ch);
            pos++;
        }

        return (pos == in.length()) ? -1 : pos;
    }
public final static int readXMLUntil ( final StringBuilder out , final String in , final int start , final char ... end ) { int pos = start ; boolean inString = false ; char stringChar = 0 ; while ( pos < in . length ( ) ) { final char ch = in . charAt ( pos ) ; if ( inString ) { if ( ch == '\\' ) { out . append ( ch ) ; pos ++ ; if ( pos < in . length ( ) ) { out . append ( ch ) ; pos ++ ; } continue ; } if ( ch == stringChar ) { inString = false ; out . append ( ch ) ; pos ++ ; continue ; } } switch ( ch ) { case '"' : case '\'' : inString = true ; stringChar = ch ; break ; } if ( ! inString ) { boolean endReached = false ; for ( int n = 0 ; n < end . length ; n ++ ) { if ( ch == end [ n ] ) { endReached = true ; break ; } } if ( endReached ) { break ; } } out . append ( ch ) ; pos ++ ; } return ( pos == in . length ( ) ) ? - 1 : pos ; }
Reads characters until any 'end' character is encountered, ignoring
escape sequences.

@param out
The StringBuilder to write to.
@param in
The Input String.
@param start
Starting position.
@param end
End characters.
@return The new position or -1 if no 'end' char was found.