Sunday, November 1, 2009

Getting raw XML when testing Axis clients

I'm sure there's probably a really easy way to get the XML using code generated by Axis, but it wasn't so easy for me to find. I was using Axis 1.5 to do UPS's Tradablility Web Services. The web services are on https, so I didn't have a lot of luck with tcpmon to watch the data. The wsdl2java tool created a Stub and a unit test. I traced the unit test and found that the stub was making the web services calls. If you are looking, find your stub and look for where the code uses the org.apache.axis2.client.OperationClient. That code block should create a org.apache.axiom.soap.SOAPEnvelope. The SOAPEnvelope will have your XML

org.apache.axiom.soap.SOAPEnvelope env = ....

To get the XML:

String xml = env.getBody().toString();

After the web service call is made, you should see the code that gets the web service response e.g.:

org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient.getMessageContext(org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);

Then you can get the response XML:

org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();

String xml = _returnEnv.getBody().toString();

I hope that makes sense.

1 comment:

Amrendra INDIA said...

Getting exception------
org.apache.axiom.om.OMException: java.util.NoSuchElementException
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:249)
at org.apache.axiom.om.impl.llom.OMElementImpl.buildNext(OMElementImpl.java:633)
at org.apache.axiom.om.impl.llom.OMElementImpl.getFirstOMChild(OMElementImpl.java:650)
at org.apache.axiom.om.impl.llom.OMElementImpl.getChildren(OMElementImpl.java:323)
at org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:903)
at org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:889)
at org.apache.axiom.om.impl.llom.OMElementImpl.internalSerialize(OMElementImpl.java:905)
at ———
———

Caused by: java.util.NoSuchElementException
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1083)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:506)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:161)
... 15 more