报文解析

用HttpClinet 来获取响应报文,document文档解析响应报文

       为了结构明确,层次分明,我将冗长的方法封装在了多个方法中,在主方法中体现功能调用顺序,这样思路更清晰些。大家可以看主方法的中的调用顺序进行学习。调用方法出用红色字体标注.思路由黄色底色标注。

  1. public static void main(String[] args) {

  2. StringBuilder soap=new StringBuilder(); //构造请求报文

  3. soap.append(" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ");

  4. soap.append(" xmlns:wor=\"http://www.horizon.com/workflow/webservice/client/workflowCommon\">");

  5. soap.append(" <soapenv:Header>");

  6. soap.append(" <HZWFService xmlns=\"http://www.huizhengtech.com/webservice/workflow\"");

  7. soap.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"");

  8. soap.append(" SOAP-ENV:actor=\"http://www.w3.org/2003/05/soap-envelope/role/next\">admin&admin</HZWFService>");

  9. soap.append(" </soapenv:Header>");

  10. soap.append(" <soapenv:Body>");

  11. soap.append(" <wor:sysLogin>");

  12. soap.append(" <loginName>loginname</loginName >");

  13. soap.append(" <password>password</password>");

  14. soap.append(" <dbidentifier>system</dbidentifier>");

  15. soap.append(" </wor:sysLogin>");

  16. soap.append(" </soapenv:Body>");

  17. soap.append(" </soapenv:Envelope>");

  18. String requestSoap=soap.toString();

  19. String serviceAddress="http://IP:Port/WorkflowCommonService?wsdl"; //服务地址(将XXXX替换成自己项目的地址)

  20. String charSet="utf-8";

  21. String contentType="text/xml; charset=utf-8";

  22. <span style="font-size:24px;">//第一步:调用方法getResponseSoap。返回响应报文和状态码</span>

  23. Map<String,Object> responseSoapMap=SoapUtil.getResponseSoap(requestSoap, serviceAddress, charSet, contentType);

  24. Integer statusCode=(Integer)responseSoapMap.get("statusCode");

  25. if(statusCode==200){

  26. String responseSoap=(String)responseSoapMap.get("responseSoap");

  27.   String targetNodeName="isSuccess";

  28. <span style="font-size:24px;"> //第二步:调用strXmlToDocument方法。

  29. 将字符串类型的XML的响应报文 转化成Docunent结构文档</span>

  30. Document doc=XMLUtil.strXmlToDocument(responseSoap);

  31. <span style="font-size:24px;"> //第三步:调用getValueByElementName方法。递归获得目标节点的值</span>

  32. String result= XMLUtil.getValueByElementName(doc,targetNodeName);

  33. if(!StringUtils.isEmpty(result)){

  34. System.out.println(result);

  35.  }

  36. else{

  37. System.out.println("没有此节点或者没有值!");}

  38.   }

  39. else{

  40.   System.out.println("请求失败!");

  41. }

  42. }

工具类    SoapUtil.class  。    

  1. /**

  2. * <p>Description: 根据请求报文,请求服务地址获取 响应报文

  3. * @param requestSoap 请求报文

  4. * @param serviceAddress 响应报文

  5. * @param charSet 字符集

  6. * @param contentType 类型

  7. * @return map封装的 服务器响应参数和返回报文.PS:statusCode :200正常响应。responseSoap:响应报文

  8. * <p>thinking: </p>

 

  1. public static Map<String,Object> responseSoap(String requestSoap,String serviceAddress,String charSet, String contentType){

  2. String responseSoap="";

  3. Map<String,Object> resultmap=new HashMap<String,Object>();

  4. PostMethod postMethod = new PostMethod(serviceAddress);

  5. byte[] b = new byte[0];

  6. try {

  7. b = requestSoap.getBytes(charSet);

  8. } catch (UnsupportedEncodingException e) {

  9. e.printStackTrace();

  10. }

  11. InputStream is = new ByteArrayInputStream(b, 0, b.length);

  12. RequestEntity re = new InputStreamRequestEntity(is, b.length, contentType);

  13. postMethod.setRequestEntity(re);

  14. HttpClient httpClient = new HttpClient();

  15. int statusCode = 0;

  16. try {

  17. statusCode = httpClient.executeMethod(postMethod);

  18. resultmap.put("statusCode", statusCode);

  19. } catch (IOException e) {

  20. throw new RuntimeException("执行http请求失败", e);

  21. }

  22. if (statusCode == 200) {

  23. try {

  24. responseSoap = postMethod.getResponseBodyAsString();

  25. resultmap.put("responseSoap", responseSoap);

  26. } catch (IOException e) {

  27. throw new RuntimeException("获取请求返回报文失败", e);

  28. }

  29. } else {

  30. throw new RuntimeException("请求失败:" + statusCode);

  31. }

  32. return resultmap;

  33. }


工具类   XMLUtil.class

  1.     /**

  2.      * <p>Description:将字符串类型的XML 转化成Docunent文档结构</p>

  3.      * @param parseStrXml 待转换的xml 字符串

  4.      * @return Document

  5.      *

  6.      * @author  hu

  7.      */

  8. public static Document strXmlToDocument(String parseStrXml){

  9. StringReader read = new StringReader(parseStrXml);

  10. //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入

  11. InputSource source = new InputSource(read);

  12. //创建一个新的SAXBuilder

  13. SAXBuilder sb = new SAXBuilder(); // 新建立构造器

  14. Document doc = null;

  15. try {

  16. doc = sb.build(source);

  17. } catch (JDOMException e) {

  18. e.printStackTrace();

  19. } catch (IOException e) {

  20. e.printStackTrace();

  21. }

  22. return doc;

  23.  
  24. }

 

  1. /**

  2. * <p>Description: 根据目标节点名获取值</p>

  3. * @param doc 文档结构

  4. * @param finalNodeName 最终节点名

  5. * @return

  6. *

  7. * @author h

  8. */

  9. public static String getValueByElementName(Document doc,String finalNodeName){

  10. Element root = doc.getRootElement();

  11. HashMap<String,Object> map=new HashMap<String,Object>();

  12. <span style="font-size:24px;"> //调用getChildAllText方法。获取目标子节点的值 </span>

  13. Map<String,Object> resultmap=getChildAllText(doc, root,map);

  14. String result=(String)resultmap.get(finalNodeName);

  15. return result;

  16. }

 

  1. /**

  2. * <p>Description: 递归获得子节点的值</p>

  3. * @param doc 文档结构

  4. * @param e 节点元素

  5. * @param resultmap 递归将值压入map中

  6. * @return

  7. *

  8. * @author huoge

  9. */

  10. public static Map<String ,Object> getChildAllText(Document doc, Element e,HashMap<String,Object> resultmap)

  11. {

  12. if (e != null)

  13. {

  14. if (e.getChildren() != null) //如果存在子节点

  15. {

  16. List<Element> list = e.getChildren();

  17. for (Element el : list) //循环输出

  18. {

  19. if(el.getChildren().size() > 0) //如果子节点还存在子节点,则递归获取

  20. {

  21. getChildAllText(doc, el,resultmap);

  22. }

  23. else

  24. {

  25. resultmap.put(el.getName(), el.getTextTrim()); //将叶子节点值压入map

  26. }

  27. }

  28. }

  29. }

  30. return resultmap;

  31. }

这就是用webservice进行soap接口调用的思路。另外webservice接口调用还可以返回json数据格式然后进行对JSon进行解析,比较容易。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值