Search code examples
javacdataaxiomsaaj

How to add CDATA section to SOAPMessage using SAAJ?


I'm trying to add a cdata section to a soap message using saaj (axis2-saaj-1.5.4). I have an xml document which I would like to define as a cdata section and put it into an element inside the body of a soap document... something like the following (although this causes compiler errors):

Element cdataElem = doc.createElement("cdata");
CDATASection cdata = doc.createCDATASection(xmlDocAsString);
cdataElem.appendChild(cdata);

SOAPMessage message = factory.createMessage();
soapMessage.getSOAPBody().addChildElement(cdataElem);

I can't find a way to do this properly and I'm sure it should be simple... can anyone help?

Thanks


Solution

  • I've resolved this using axiom instead of saaj as suggested in by skaffman.

    I've used axiom-api-1.2.8.jar and axiom-impl-1.2.8.jar for sample code below:

    SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
    SOAPEnvelope envelope = factory.getDefaultEnvelope();
    OMElement xmlElement= factory.createOMElement("search", envelope.getDefaultNamespace());
    envelope.getBody().addChild(xmlElement);
    
    OMTextImpl omText = (OMTextImpl) xmlElement.getOMFactory().createOMText(xmlElement, xmlForCdata, XMLStreamConstants.CDATA);
    xmlElement.addChild(omText);
    
    System.out.println(envelope.toStringWithConsume());
    

    This seems to work very nicely and doesn't convert '<' tags to & lt;