简体   繁体   中英

how to get child element from SOAP body?

How can I get clientCode from requestHeader which is located under SOAP body?

<soapenv:Body>
      <ser:GS>
         <!--Optional:-->
         <requestHeader>
            <!--Optional:-->
            <req:clientCode>KL7MU</req:clientCode>
            <!--Optional:-->
            <req:clientUsername>BLABLA</req:clientUsername>
         </requestHeader>
      </ser:GS>
   </soapenv:Body>

I try to get but iterator.hasNext() returns false .

SOAPBody soapBody = context.getMessage().getSOAPBody();

java.util.Iterator iterator = soapBody.getChildElements();
while (iterator.hasNext()) {
  SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
  String val = bodyElement.getValue();
  System.out.println("The Value is:" + val);
}

You can use wsdl to generate java class:

wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl    

Then you can call SOAP services as local methods.

Some other tools:

wsdl2javawizard: http://sourceforge.net/projects/wsdl2javawizard/

apache cxf: http://cxf.apache.org/docs/wsdl-to-java.html

instead of getting form SOAP message body try the child elements form SOAP message header like context.getMessage().getSOAPPart().getEnvelope().getHeader(); details : http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/

You can get the values like in axiom,

SOAPEnvelope mes = messageContext.getEnvelope();
SOAPHeader mesh = mes.getHeader();
SOAPBody mesb = mes.getBody();
OMElement messageId = mesh.getFirstChildWithName(new QName("http://www.w3.org/2005/08/addressing","MessageID"));
String messageIDStr = messageId.getText();
OMElement bodyChild = mesb.getFirstElement();
OMElement remoteAddress = bodyChild.getFirstChildWithName(new QName(
                                    "http://YourNameSpaceURI",
                                    "remoteAddress"));
String remoteAddressStr = remoteAddress.getText();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM