title: How to cast SAAJ object during handling SOAP message?
body: <p>Colleagues, i have xml which need to be handle and configure before sending to client:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;soapenv:Body&gt;
          &lt;ns2:request xmlns:ns2="http://mayacomp/Generic/Ws"&gt;
              &lt;in xmlns="http://mayacomp/Generic/Ws"&gt;
                  &lt;requestHeader&gt;
                      &lt;requestUid&gt;1234567890&lt;/requestUid&gt;
                      &lt;ns2:requestTimestamp&gt;2015-02-11T07:46:00.000&lt;/ns2:requestTimestamp&gt;
                  &lt;/requestHeader&gt;
                  &lt;ns2:FullDataView&gt;
                      &lt;ns2:ApplicationData&gt;
                          &lt;ns2:ProductType&gt;0&lt;/ns2:ProductType&gt;
                      &lt;/ns2:ApplicationData&gt;
                      &lt;ns2:ProductDetails&gt;
                          &lt;ns2:ProductCode&gt;605&lt;/ns2:ProductCode&gt;
                      &lt;/ns2:ProductDetails&gt;
                      &lt;ns2:Applicant&gt;
                          &lt;ns2:Surname&gt;Клавиатурова&lt;/ns2:Surname&gt;
                          &lt;ns2:Address&gt;
                              &lt;ns2:Type&gt;1&lt;/ns2:Type&gt;
                          &lt;/ns2:Address&gt;
                          &lt;ns2:Address&gt;
                              &lt;ns2:Type&gt;2&lt;/ns2:Type&gt;
                          &lt;/ns2:Address&gt;
                          &lt;ns2:Email&gt;test@test.ru&lt;/ns2:Email&gt;
                      &lt;/ns2:Applicant&gt;
                  &lt;/ns2:FullDataView&gt;
              &lt;/in&gt;
          &lt;/ns2:request&gt;
      &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I use the next method to configure soap message:</p>
  
  <pre><code>public void doWithMessage(WebServiceMessage message) {
  
                          SaajSoapMessage saajSoapMessage = (SaajSoapMessage)message;
  
                      try {
  
                          SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
                          SOAPPart soapPart = soapMessage.getSOAPPart();
                          SOAPEnvelope envelope = soapPart.getEnvelope();
                          log.info("SOAPEnvelope prefix " +  envelope.getPrefix());
                          envelope.removeNamespaceDeclaration(envelope.getPrefix());
                          envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                          envelope.setPrefix("soapenv");
  
                          /*Remove header*/
                          SOAPHeader header = soapMessage.getSOAPHeader();
                          header.detachNode();
  
  
                          SOAPBody body = envelope.getBody();
                          log.info("SOAPBody prefix " +  body.getPrefix());
                          body.removeNamespaceDeclaration(body.getPrefix()) ;
                          body.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                          body.setPrefix("soapenv");
  
  
                          /*Prepare body elements*/
                          Iterator itBodyElements = body.getChildElements();
  
                          String elementName = null;
                          /*Log all elements from body*/
                          log.info("Body elements: ");
                          NodeList nodeList = body.getElementsByTagName("*") ;
                          for (int i = 0; i &lt; nodeList.getLength(); i++) {
                              Node node = nodeList.item(i);
                              if (node.getNodeType() == Node.ELEMENT_NODE) {
                                  log.info(node.getNodeName());
                              }
                          }
  
                          /*Config body elements*/
                          while (itBodyElements.hasNext()) 
                          { 
                            Object o = itBodyElements.next();
                            SOAPBodyElement bodyElement = (SOAPBodyElement) o;
                            log.info("Elements from 'Body' element = " + bodyElement.getLocalName() );
  
                            Iterator it2 = bodyElement.getChildElements();
                                   while (it2.hasNext()) 
                                   { 
                                        Object requestElement = it2.next();
                                        SOAPBodyElement bodyRequest = (SOAPBodyElement) requestElement;
                                        log.info("  Elements from '"+ bodyElement.getLocalName() + "' element = " + bodyRequest.getLocalName()); 
                                        bodyRequest.removeNamespaceDeclaration(bodyRequest.getPrefix());
                                        bodyRequest.setPrefix("");
  
                                         Iterator it3 = bodyRequest.getChildElements();
                                              while (it3.hasNext())
                                              {
                                                Object bodyRequestElement = it3.next();
                                                SOAPBodyElement requestElementName = (SOAPBodyElement) bodyRequestElement;
                                                log.info("   Elements from 'In' name = " + requestElementName.getLocalName());
                                                requestElementName.setPrefix("");
                                                elementName =  requestElementName.getLocalName();
  
                                                Iterator it4  = requestElementName.getChildElements();                                              
  
                                                  while (it4.hasNext()) 
                                                            {
                                                                Object o4 = it4.next();
                                                                SOAPBodyElement bodyElement4 = (SOAPBodyElement) o4;
                                                                log.info("    Element from '" + elementName + "' name = " + bodyElement4.getLocalName());
                                                                bodyElement4.setPrefix("");
  
                                                              Iterator it5 =  bodyElement4.getChildElements(); 
  
                                                              while (it5.hasNext())
                                                              {
                                                                  Object o5 = it5.next(); 
                                                                  //What it tried?
                                                              // TextImpl child5 = (TextImpl) o5; //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl cannot be cast to com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl
                                                              // ElementImpl child5 = (ElementImpl) o5; //Result:java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl cannot be cast to com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl
                                                              // SOAPElement child5 = (SOAPElement)o5; //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPElement
                                                              // SOAPBodyElement child5 = (SOAPBodyElement) o5;  //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPBodyElement
                                                                  log.info(o5.getClass().toString());
                                                              }
                                                             }
                                              }
                                   }
  
                         } 
                       } catch (Exception e) {
                          e.printStackTrace();
                       }
                  }
  </code></pre>
  
  <p>The problem is that i can not cast the Object o5 (<code>class com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl</code>) to any types described in code.
  How correctly cast the object o5?</p>
  
  <p>Thank you.</p>