SoapUtils { public static String extractNonEmptyElement(String document, String elementName) { 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(); static Element getElementNS(
            Element root,
            Set<String> nsUris,
            String wantedLocalName); static String extractSoapBody(String soapEnvelope); static String extractNonEmptyElement(String document, String elementName); static Exception extractOutgoingException(Exchange exchange); static String extractOutgoingPayload(Exchange exchange); static final Set<String> WS_ADDRESSING_NS_URIS; static final Set<String> SOAP_NS_URIS; }
@Test public void testExtractNamedElement() { final String elementName = "ImportantQuestion"; assertEquals(contents, extractNonEmptyElement(envelopeWithNamespacePrefixes, elementName)); assertEquals(contents, extractNonEmptyElement(envelopeWithoutNamespacePrefixes, elementName)); assertEquals(null, extractNonEmptyElement(emptyEnvelopeWithNamespacePrefixes, elementName)); assertEquals(null, extractNonEmptyElement(emptyEnvelopeWithoutNamespacePrefixes, elementName)); assertEquals(null, extractNonEmptyElement(emptyEnvelopeWithNamespacePrefixesShort, elementName)); assertEquals(null, extractNonEmptyElement(emptyEnvelopeWithoutNamespacePrefixesShort, elementName)); Assert.assertNull(extractNonEmptyElement(null, elementName)); Assert.assertNull(extractNonEmptyElement(totallyBad, elementName)); }