您的位置:首页 > 其它

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

2015-04-03 00:00 309 查看
//      用工厂类创建一个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();
}*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: