class SoapUtils {

    public static String extractNonEmptyElement(String document, String elementName) {

        // ... <prefix:elementName attr1="abcd"> ... </prefix:elementName> ...
        //     3                                     2        1

        try {
            int pos1 = document.lastIndexOf(elementName + '>');
            if (pos1 < 0) {
                LOG.warn("Cannot find end of the closing tag of {}", elementName);
                return null;
            }

            int pos2 = document.lastIndexOf('<', pos1 - 1);
            if (pos2 < 0) {
                LOG.warn("Cannot find start of the closing tag of {}", elementName);
                return null;
            }

            StringBuilder sb = new StringBuilder().append('<');
            if (pos1 - pos2 > 2) {
                sb.append(document, pos2 + 2, pos1 - 1).append(':');
            }
            int pos3 = document.indexOf(sb.append(elementName).toString());
            if (pos3 < 0) {
                LOG.warn("Cannot find start of the opening tag of {}", elementName);
                return null;
            }

            return document.substring(pos3, pos1 + elementName.length() + 1);

        } catch (Exception e) {
            LOG.error("Could not extract element" + elementName, e);
            return null;
        }
    }

    private  SoapUtils();

}