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.crea开发者_StackOverflow中文版teMessage();
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
I had a similar problem. Finally I got a solution using saaj:
SOAPMessage message = factory.createMessage();
message.getSOAPBody().addChildElement(message.getSOAPpart().createCDATASection(xmlDocAsString));
Apparentetly SOAPPart seems to act as a DOM doc.
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;
I managed to add a CDATA section with SAAJ by simply surrounding the root element of my XML with a CDATA section and then adding the XML to the element with "element.addTextNode(xmlString)"
As a result the XML entities were not escaped as they are without the CDATA section.
Cheers, Torsten
精彩评论