程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 使用axis發送xml報文,返回並解析報文實例,axisxml

使用axis發送xml報文,返回並解析報文實例,axisxml

編輯:JAVA綜合教程

使用axis發送xml報文,返回並解析報文實例,axisxml


前段時間剛好學了一點webservice,因此想和大家分享一下我的成果,因為能力原因,只能做個小實例,望大家見諒!

此實例的思路是:(1)用String類型構造好需要發送的報文;(2)使用axis調用服務發送報文; (3)將返回的報文進行解析。

如下是axis所使用的包:

接下來是實例代碼:

  1 //測試實例
  2 public class test {
  3 
  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 }
 37     
 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();
 49 
 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             }
134             
135         
136         
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);
151                 
152             }catch(Exception e){
153                 //System.out.println(e);
154             }
155             return doc;
156         }
157 }

當然,發送的報文格式因他人提供的文檔類型定義而不同,故解析報文的方法也不同,但大體思路如此,故此代碼只供參考。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved