File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes FRom Element to SOAP Envelope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "FRom Element to SOAP Envelope" Watch "FRom Element to SOAP Envelope" New topic

FRom Element to SOAP Envelope

taktak hajer

Joined: Sep 24, 2012
Posts: 3

I want to get SOAPEnvelope from a DOM document. I wrote this class :

public static SOAPEnvelope getSOAPEnvelopeFromDOMDocument(Document doc, boolean useDoom)
throws WSSecurityException {

Element documentElement = doc.getDocumentElement();
if (documentElement instanceof SOAPEnvelope) {
SOAPEnvelope env = (SOAPEnvelope)documentElement;
// If the DOM tree already implements the Axiom API and the corresponding
// Axiom implementation is also used as default implementation, then just return
// the SOAPEnvelope directly. Note that this will never be the case for DOOM,
// but may be the case for a non standard Axiom implementation.
if (env.getOMFactory().getMetaFactory() == OMAbstractFactory.getMetaFactory()) {
return env;

if(useDoom) {
try {
//Get processed headers
SOAPEnvelope env = (SOAPEnvelope)doc.getDocumentElement();
ArrayList processedHeaderQNames = new ArrayList();
SOAPHeader soapHeader = env.getHeader();

if(soapHeader != null) {
Iterator headerBlocs = soapHeader.getChildElements();
while (headerBlocs.hasNext()) {

OMElement element = (OMElement);
SOAPHeaderBlock header = null;

if (element instanceof SOAPHeaderBlock) {
header = (SOAPHeaderBlock) element;

// If a header block is not an instance of SOAPHeaderBlock, it means that
// it is a header we have added in rampart eg. EncryptedHeader and should
// be converted to SOAPHeaderBlock for processing
} else {
header = soapHeader.addHeaderBlock(element.getLocalName(), element.getNamespace());
Iterator attrIter = element.getAllAttributes();
while (attrIter.hasNext()) {
OMAttribute attr = (OMAttribute);
header.addAttribute(attr.getLocalName(), attr.getAttributeValue(), attr.getNamespace());
Iterator nsIter = element.getAllDeclaredNamespaces();
while (nsIter.hasNext()) {
OMNamespace ns = (OMNamespace);
// retrieve all child nodes (including any text nodes)
// and re-attach to header block
Iterator children = element.getChildren();
while (children.hasNext()) {
OMNode child = (OMNode);




if(header.isProcessed()) {

XMLStreamReader reader = ((OMElement) doc.getDocumentElement())
SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(
SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();

//Set the processed flag of the processed headers
SOAPHeader header = envelope.getHeader();
for (Iterator iter = processedHeaderQNames.iterator(); iter
.hasNext();) {
QName name = (QName);
Iterator omKids = header.getChildrenWithName(name);
if(omKids.hasNext()) {

return envelope;

} catch (FactoryConfigurationError e) {
throw new WSSecurityException(e.getMessage());
} else {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLUtils.outputDOM(doc.getDocumentElement(), os, true);
ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());

SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createSOAPModelBuilder(bais, null);
return stAXSOAPModelBuilder.getSOAPEnvelope();
} catch (Exception e) {
throw new WSSecurityException(e.getMessage());

The problem concern the cast every time i execute, i got this error : xception in thread "main" java.lang.ClassCastException: cannot be cast to org.apache.axiom.soap.SOAPEnvelope

Any help please :''((

I agree. Here's the link:
subject: FRom Element to SOAP Envelope