生成XML Axis调用webservice传送XML数据过去

//      用工厂类创建一个document实例 
   Document doc = DocumentHelper.createDocument(); 
//   创建根元素
   Element root = doc.addElement("synchroFlowReq");
   //创建head
   Element head = root.addElement("head");
   //创建synchroFlow
   Element synchroFlow = head.addElement("synchroFlow");
   //创建name
   Element name = synchroFlow.addElement("name");
   //复制值
   name.setText("审批流程名称");
   //创建sender
   Element sender = synchroFlow.addElement("sender");
   //创建origin
   Element origin = sender.addElement("origin");
   origin.setText("发送节点代码");
            //创建origin
   Element processDefNm = sender.addElement("processDefNm");
   processDefNm.setText("模板名称");
   //创建processInsId
   Element processInsId = sender.addElement("processInsId");
   processInsId.setText("流程实例ID");
            //创建processInsId
   Element activityDefId = sender.addElement("activityDefId");
   activityDefId.setText("节点活动定义ID");
   
   /**
    * 生成XML
    */
   OutputFormat format = OutputFormat.createPrettyPrint(); 
   format.setEncoding("UTF-8");  
   //创建写出对象  
   XMLWriter writer;
   String path;
   try {
                //指定xml文件生成目录
       path = TimerTaskUtil.class.getResource("/").getPath();
       System.out.println(path);
    path = path.substring(0, path.indexOf("classes/"));
    writer = new XMLWriter(new FileOutputStream(path+"XMLFile/"+this.id+new Date()+".xml"),format);
    writer.write(doc);
    
    Service s = new  Service();
    Call call = (Call) s.createCall();
    call.setUseSOAPAction(true);
    
    String method[] = (this.counts.trim().toString()).split("\\|");
    
    
    call.setOperationName(new QName(method[1], method[2]));
    call.addParameter("arg1", XMLType.XSD_STRING, ParameterMode.IN);
    call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
    call.setTargetEndpointAddress(method[0]);
    String ret = (String) call.invoke(new Object[] { doc.asXML() });
    write(ret, path+"XMLFile/"+this.id+new Date()+".xml");
    String status = xmlElements(ret);
    if(status.equals("200")){
     System.out.println("WEBSERVICE成功!"+new Date());
    }else{
     System.out.println("WEBSERVICE失败!"+new Date());
    }
    
   } catch (IOException e) {
     // TODO Auto-generated catch block
   e.printStackTrace(); 
   } catch (ServiceException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 /*
//      服务的地址
         URL wsUrl;
   try {
    wsUrl = new URL("http://192.168.1.100:6789/hello");
    HttpURLConnection conn;
    conn = (HttpURLConnection) wsUrl.openConnection();
    conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
          OutputStream os = conn.getOutputStream();
          
          //请求体
          String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
                        "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>";
          
          os.write(soap.getBytes());
          
          InputStream is = conn.getInputStream();
          
          byte[] b = new byte[1024];
          int len = 0;
          String s = "";
          while((len = is.read(b)) != -1){
              String ss = new String(b,0,len,"UTF-8");
              s += ss;
          }
          System.out.println(s);
          is.close();
          os.close();
          conn.disconnect();
          
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();  
         
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }*/
        
     }

转载于:https://my.oschina.net/u/2307850/blog/395758

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值