This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FRom Element to SOAP Envelope

taktak hajer
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 :''((

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic