相关文章推荐
善良的刺猬  ·  C# ...·  1 年前    · 

前段时间刚好学了一点webservice,因此想和大家分享一下我的成果,因为能力原因,只能做个小实例,望大家见谅!

此实例的思路是:(1)用String类型构造好需要发送的报文;(2)使用axis调用服务发送报文; (3)将返回的报文进行解析。

如下是axis所使用的包:

接下来是实例代码:

  1 //测试实例
  2 public class test {
  4     public static void main(String[] args) {
  5         // TODO Auto-generated method stub
  6         test t=new test();
  7         //发送的报文
  8         String requestXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><service xmlns=\"http://www.chinatax.gov.cn/spec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><head><tran_id>SWNDPJ</tran_id><channel_id>SWZJ</channel_id><tran_seq>sdfas123</tran_seq><tran_date>20151102</tran_date><tran_time>14494777</tran_time><rtn_code>0</rtn_code><rtn_msg><Code>0000</Code><Message>交易成功</Message><Reason>获取纳税人年度评价结果成功。</Reason></rtn_msg><expand><name>identityType</name><value></value></expand><expand><name>sjry</name><value></value></expand><expand><name>sjjg</name><value></value></expand></head><body><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><taxML xsi:type=\"nsxyPjxxYwbw\"  bbh=\"v1.0\" xmlbh=\"String\" xmlmc=\"String\" xsi:schemaLocation=\"http://www.chinatax.gov.cn/dataspec/TaxMLBw_NSXY_PJXX_00001_Response_V1.0.xsd\" xmlns=\"http://www.chinatax.gov.cn/dataspec/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><nsxypjxx><nsxypjxxHead><type>NSXY</type><pjlx>NSXY</pjlx></nsxypjxxHead><nsxypjxxBody><nsrsbh>4406817919</nsrsbh><nsrmc></nsrmc><pjnd>2015</pjnd><pjjb></pjjb><pjfs></pjfs><sflhpj>1</sflhpj></nsxypjxxBody></nsxypjxx></taxML>]]></body></service>";;
  9         String responseXml=t.getResponseXml(requestXml);
 10         Map xmlMap=t.parseXml2Map(responseXml);
 11         System.out.println(xmlMap);
 12     }
 13     /**
 14      * 使用axis调用接口发送报文
 15      * @param requestXml 发送的报文
 16      * @return responseXml 返回的报文
 17      * @throws Exception
 18      */
 19     public static String getResponseXml(String requestXml) throws Exception{
 20         String url = "";//输入TargetEndpointAddress的地址
 21         String TargetEndpointAddress = url;
 22         String TargetNamespace = "";//输入TargetNamespace
 23         Service service = new Service();
 24         String responseXML = "";
 25         Call call = null;
 26         call = (Call) service.createCall();
 27         QName qn = new QName(TargetNamespace, "doService");//doService为调用的服务里的方法
 28         call.setTargetEndpointAddress(TargetEndpointAddress);
 29         call.addParameter("parameters", qn, ParameterMode.IN);
 30         call.setReturnType(XMLType.XSD_STRING);
 31         call.setOperationName(qn);
 32         String[] parameters = { requestXml };
 33         responseXML = (String) call.invoke(parameters);
 34         System.out.println(responseXML);
 35     return responseXML;
 36 }
 38     /**
 39      * 解析返回的报文数据 responseXml
 40      * @author Administrator
 41      *
 42      */
 43     @SuppressWarnings({ "static-access", "rawtypes", "unused", "unchecked" })
 44     public Map parseXml2Map(String xml){
 45             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //得到DOM解析器的工厂实例
 46             DocumentBuilder builder;
 47         }
 48             Map pjMap = new HashMap();
 50             Document document = ParseXml2Doc(xml);
 51             Element root = document.getDocumentElement();
 52             //解析body体报文
 53             Node    body =root.getElementsByTagName("body").item(0);
 54             String bodyxml = body.getTextContent();
 55             Document bodyDoc = ParseXml2Doc(bodyxml);
 56             Element taxMLElement = bodyDoc.getDocumentElement();
 57             NodeList taxMLList = taxMLElement.getChildNodes();
 58             for (int i = 0; i < taxMLList.getLength(); i++) {
 59                 // 是节点,进入节点下,否则不进
 60                 if (taxMLList.item(i) instanceof Element) {
 61                     String cc = taxMLList.item(i).getNodeName();
 62                     NodeList swsxList = taxMLList.item(i).getChildNodes();
 63                     // if ("nsxypjxxBody".equals(cc)) {
 64                     for (int j = 0; j < swsxList.getLength(); j++) {
 65                         if (swsxList.item(j) instanceof Element) {
 66                             String bdmc = swsxList.item(j).getNodeName();
 67                             if ("nsxypjxxBody".equals(bdmc)) {
 68                                 NodeList pjxxList = swsxList.item(j)
 69                                         .getChildNodes();
 70                                 for (int z = 0; z < pjxxList.getLength(); z++) {
 71                                     String pjxx = pjxxList.item(z).getNodeName();
 72                                     if ("djxh".equals(pjxx)) {
 73                                         pjMap.put("djxh", pjxxList.item(z)
 74                                                 .getTextContent());
 75                                     }
 76                                     else if ("nsrsbh".equals(pjxx)) {
 77                                         pjMap.put("nsrsbh", pjxxList.item(z)
 78                                                 .getTextContent());
 79                                     }
 80                                     else if ("nsrmc".equals(pjxx)) {
 81                                         pjMap.put("nsrmc", pjxxList.item(z)
 82                                                 .getTextContent());
 83                                     }
 84                                     else if ("pjnd".equals(pjxx)) {
 85                                         pjMap.put("pjnd", pjxxList.item(z)
 86                                                 .getTextContent());
 87                                     }
 88                                     else if ("pjjb".equals(pjxx)) {
 89                                         pjMap.put("pjjb", pjxxList.item(z)
 90                                                 .getTextContent());
 91                                     }else if ("pjfs".equals(pjxx)) {
 92                                         pjMap.put("pjfs", pjxxList.item(z)
 93                                                 .getTextContent());
 94                                     }else if ("pjdf".equals(pjxx)) {
 95                                         pjMap.put("pjdf", pjxxList.item(z)
 96                                                 .getTextContent());
 97                                     }else if ("qpf".equals(pjxx)) {
 98                                         pjMap.put("qpf", pjxxList.item(z)
 99                                                 .getTextContent());
100                                     }else if ("sflhpj".equals(pjxx)) {
101                                         pjMap.put("sflhpj", pjxxList.item(z)
102                                                 .getTextContent());
103                                     }
104                                 }
105                             }
106                         }
107                     }
108                 }
109             }
110             //解析head体报文
111             Node headNode=root.getElementsByTagName("head").item(0);
112             NodeList headList=headNode.getChildNodes();
113             Node rtn_msgNode=null;
114             for(int j=0;j<headList.getLength();j++){
115                 rtn_msgNode=headList.item(j);
116                 if(rtn_msgNode.getNodeName().equals("rtn_msg")){
117                     NodeList rtn_msgList=rtn_msgNode.getChildNodes();
118                     Node rtn_msgNode2=null;
119                     if(rtn_msgList instanceof Element){
120                         for(int i=0;i<rtn_msgList.getLength();i++){
121                             rtn_msgNode2=rtn_msgList.item(i);
122                             if("Code".equals(rtn_msgNode2.getNodeName())){
123                                 pjMap.put("Code", rtn_msgNode2.getTextContent());
124                             }else if("Message".equals(rtn_msgNode2.getNodeName())){
125                                 pjMap.put("Message", rtn_msgNode2.getTextContent());
126                             }else if("Reason".equals(rtn_msgNode2.getNodeName())){
127                                 pjMap.put("Reason", rtn_msgNode2.getTextContent());
128                             }
129                         }
130                     }
131                 }
132                 return pjMap;
133             }
137         /**
138          * 将xml加载为document对象
139          * @param xmlStr
140          * @return
141          */
142         public Document ParseXml2Doc(String xmlStr){
143             ByteArrayInputStream in;
144             if("".equals(xmlStr))
145                 return null;
146             Document doc = null;
147             try{
148                 builder=factory.newDocumentBuilder(); 
149                 in=new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
150                 doc=builder.parse(in);
152             }catch(Exception e){
153                 //System.out.println(e);
154             }
155             return doc;
156         }
157 }

当然,发送的报文格式因他人提供的文档类型定义而不同,故解析报文的方法也不同,但大体思路如此,故此代码只供参考。