从SOAP响应中获取价值 [英] Get Value from SOAP response

查看:159
本文介绍了从SOAP响应中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一句话:

SOAPMessage soapResponse = soapConnection.call(message,url);

并且回复看起来:

HTTP/1.1 200 OK
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 24 Jul 2013 07:44:39 GMT
Server: Apache-Coyote/1.1

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Header>
  <TransactionID soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="1" xmlns="http://somelink"></TransactionID>
 </soapenv:Header>
 <soapenv:Body>
  <soap-env:Fault xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:faultcode>Server</soap-env:faultcode>
   <soap-env:faultstring>Server Error</soap-env:faultstring>
   <soap-env:Detail>
    <soap-env:Status>
     <soap-env:StatusCode>3000</soap-env:StatusCode>
     <soap-env:StatusText>Server Error</soap-env:StatusText>
     <soap-env:Details></soap-env:Details>
    </soap-env:Status>
   </soap-env:Detail>
  </soap-env:Fault>
 </soapenv:Body>
</soapenv:Envelope>

如何从这样的soap响应中获取String中的StatusCode(3000)?
i试过 soapResponse.getSOAPBody().... 但我能得到的是:状态

how can i get StatusCode (3000) in String from such a soap response? i tried soapResponse.getSOAPBody().... but all i could get was :status

编辑:

所以我做了:

  Detail detail = soapResponse.getSOAPPart().getEnvelope().getBody().getFault().getDetail();
 Iterator detailEntries = detail.getDetailEntries();
 while (detailEntries.hasNext()) {
     SOAPBodyElement bodyElement = (SOAPBodyElement) detailEntries.next();
     Iterator val = bodyElement.getChildElements();
     while (val.hasNext()) {
         SOAPBodyElement bodyElement2 = (SOAPBodyElement) val.next();
         String val2 = bodyElement2.getValue();
         logger.debug("The Value is:" + val2);
     }

但是获得了类强制转换异常
}
Edit2:解决方案:

but got class cast exception } Solution:

soapResponse.getSOAPPart().getEnvelope().getBody().getFault().getDetail().getTextContent().trim().substring(0, 4));


推荐答案


  • 从< a href =http://docs.oracle.com/javase/7/docs/api/javax/xml/soap/SOAPMessage.html\"rel =nofollow> SOAPMessage ,你需要调用 SOAPMessage#getSOAPPart() 获取 SOAPPart

  • 通过致电 SOAPPart#getEnvelope() 你可以获得 SOAPEnvelope

  • 接下来你得到 SOAPBo dy 使用 SOAPEnvelope #getBody()

  • 现在你可以得到 SOAPFault 通过调用 SOAPBody #getFault()

  • 接下来,您调用 SOAPFault#getDetail() 获取 明细

  • 使用 详细信息#getDetailIterator() 你可以迭代ov所有的 DetailEntry 详细信息对象。

  • 由于 DetailEntry 接口扩展了 SOAPElement 界面,您可以通过调用 getChildElements() ;这样你就可以导航到 Status 元素中的 StatusCode 元素并获取它的值。

    • Starting from the SOAPMessage, you need to call SOAPMessage#getSOAPPart() to obtain a SOAPPart.
    • By calling SOAPPart#getEnvelope() you can obtain the SOAPEnvelope.
    • Next you get the SOAPBody using SOAPEnvelope#getBody().
    • Now you can get the SOAPFault by calling SOAPBody#getFault().
    • Next, you call SOAPFault#getDetail() to obtain the Detail.
    • Using Detail#getDetailIterator() you can iterate over all of the DetailEntrys in the Detail object.
    • Since the DetailEntry interface extends the SOAPElement interface, you can get its content by calling getChildElements(); that way you can navigate to the StatusCode element inside the Status element and get its value.
    • 这篇关于从SOAP响应中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆