首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

axis调用中国气象局web服务,用dom4j解析的事例

2013-07-09 
axis调用中国气象局web服务,用dom4j解析的例子Java代码??package?service.cilent????import?java.util.It

axis调用中国气象局web服务,用dom4j解析的例子
Java代码??axis调用中国气象局web服务,用dom4j解析的事例

  1. package?service.cilent;??
  2. ??
  3. import?java.util.Iterator;??
  4. import?org.apache.axiom.om.OMAbstractFactory;??
  5. import?org.apache.axiom.om.OMElement;??
  6. import?org.apache.axiom.om.OMFactory;??
  7. import?org.apache.axiom.om.OMNamespace;??
  8. import?org.apache.axiom.soap.SOAP11Constants;??
  9. import?org.apache.axis2.Constants;??
  10. import?org.apache.axis2.addressing.EndpointReference;??
  11. import?org.apache.axis2.client.Options;??
  12. import?org.apache.axis2.client.ServiceClient;??
  13. import?org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;??
  14. import?org.dom4j.Document;??
  15. import?org.dom4j.DocumentHelper;??
  16. import?org.dom4j.Element;??
  17. ??
  18. ??
  19. public?class?JavaServicesClient?{??
  20. ????private?static?EndpointReference?targetEPR?=?new?EndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");??
  21. ??
  22. ????@SuppressWarnings("unchecked")??
  23. ????public?void?getResult()?throws?Exception?{??
  24. ????????ServiceClient?sender?=?new?ServiceClient();??
  25. ????????sender.setOptions(buildOptions());??
  26. ????????//得到axis2定义的xml文件格式??
  27. ????????OMElement?result?=?sender.sendReceive(buildParam());??
  28. ????????//将axis2的xml格式转换为dom的为XML格式???
  29. ????????Document?doc?=?DocumentHelper.parseText(result.toString());????
  30. ????????//获取根节点??
  31. ????????Element?rootElt?=?doc.getRootElement();???
  32. ????????//?获取根节点下的getWeatherbyCityNameResult子节点??
  33. ????????Iterator?iter?=?rootElt.elementIterator("getWeatherbyCityNameResult");???
  34. ????????while(iter.hasNext()){??
  35. ????????????Element?recordEle?=?(Element)?iter.next();??
  36. ????????????//?获取子节点getWeatherbyCityNameResult下的子节点string??
  37. ????????????Iterator?iters?=?recordEle.elementIterator("string");???
  38. ????????????while(iters.hasNext()){??
  39. ????????????????Element?itemEle?=?(Element)?iters.next();??
  40. ????????????????//输出string的值??
  41. ????????????????System.out.println(itemEle.getTextTrim());??
  42. ????????????}??
  43. ????????}??
  44. ????}??
  45. ??
  46. ????private?static?OMElement?buildParam()?{??
  47. ????????OMFactory?fac?=?OMAbstractFactory.getOMFactory();??
  48. ????????OMNamespace?omNs?=?fac.createOMNamespace("http://WebXml.com.cn/",?"");??
  49. ????????OMElement?data?=?fac.createOMElement("getWeatherbyCityName",?omNs);??
  50. ????????OMElement?inner?=?fac.createOMElement("theCityName",?omNs);??
  51. ????????inner.setText("深圳");??
  52. ????????data.addChild(inner);??
  53. ????????return?data;??
  54. ????}??
  55. ??
  56. ????private?static?Options?buildOptions()?{??
  57. ????????Options?options?=?new?Options();??
  58. ????????options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);??
  59. ????????options.setAction("http://WebXml.com.cn/getWeatherbyCityName");??
  60. ??
  61. ????????options.setTo(targetEPR);??
  62. ????????//?options.setProperty?如果不是通过代理上网,此句可省??
  63. ????????//?options.setProperty(HTTPConstants.PROXY,?buildProxy());??
  64. ????????options.setTransportInProtocol(Constants.TRANSPORT_HTTP);??
  65. ????????return?options;??
  66. ????}??
  67. ??
  68. ????/**?
  69. ?????*?本机采用代理服务器上网时,需要设置代理?
  70. ?????*??
  71. ?????*?@return?
  72. ?????*/??
  73. ????public?static?ProxyProperties?buildProxy()?{??
  74. ????????ProxyProperties?proxyProperties?=?new?ProxyProperties();??
  75. ????????proxyProperties.setProxyName("代理名称");??
  76. ????????proxyProperties.setProxyPort(8080);??
  77. ????????return?proxyProperties;??
  78. ????}??
  79. ??
  80. ????public?static?void?main(String[]?args)?throws?Exception?{??
  81. ????????JavaServicesClient?s?=?new?JavaServicesClient();??
  82. ????????s.getResult();??
  83. ????}??
  84. } ?

热点排行