如何从SOAP体中获取子元素?

时间:2013-09-13 07:51:09

标签: java soap

如何从位于SOAP正文下的requestHeader获取clientCode?

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

我试着去,但iterator.hasNext()返回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);
}

3 个答案:

答案 0 :(得分:1)

您可以使用wsdl生成java类:

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

然后,您可以将SOAP服务作为本地方法调用。

其他一些工具:

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

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

答案 1 :(得分:0)

而不是形成SOAP消息体 尝试使用context元素形成SOAP消息头,例如context.getMessage()。getSOAPPart()。getEnvelope()。getHeader(); 详情:http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/

答案 2 :(得分:0)

您可以获取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();