那你可不可以读取对象中的数据构造xml呢?
解决方案 »
- struts2加入拦截器后页面取不了值
- 到底错到哪了
- Struts2 TypeConverter 转换后没有赋给Action值
- 关于Hibernate 的Lazy=true的问题
- weblogic启动时报Not enough space是为什么?
- jfreechart 高手快过来看看 怎么和数据库联
- tomcat ssl问题,请大家帮忙啊 “即将通过安全连接查看网页,该网站上的其他任何人都无法看到您与该站点的交换信息。”
- hibernate 超级难问题?大家帮我
- 求一功能:用jspSmartUpload组件下载文件,能否指定下载的客户端目录和文件名称,不出现下载提示框,就能完成下载文件呢?
- 什么是tomcat?
- Struts1.2 中配置文件中的"Action"的"attribute"属性起什么作用呀?
- 小弟初学hibernate麻烦问大家几个问题!在线等!
---------------------------
/**
*
*/
package org.srit.smdp.util.ws;import java.util.Iterator;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;import org.srit.smdp.portal.api.SMDPException;/**
* @author Quan
*
*/
public class WSUserClient {
/**
* 调用SOAP 获取请求
* @param functionName
* @param xmlns
* @param ma
* @return
*/
private String getWSRequest(String functionName, String xmlns, Map ma) { StringBuffer stbuffer = new StringBuffer();
stbuffer.append("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
stbuffer
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
stbuffer.append("<soap:Body>");
stbuffer.append("<" + functionName + " xmlns=\"" + xmlns + "\">"); if (ma != null) {
Iterator ite = ma.keySet().iterator();
Object key = "";
while (ite.hasNext()) { key = ite.next();
stbuffer.append("<" + key + ">" + ma.get(key) + "</" + key
+ ">"); }
} stbuffer.append("</" + functionName + ">");
stbuffer.append("</soap:Body>");
stbuffer.append("</soap:Envelope>\r\n"); return stbuffer.toString();//以字符串的形式输出xml
} /**
* 完成Webservice查询
* @param wsurl
* @param wsaction
* @param functionName
* @param xmlns
* @param ma
* @return
* @throws SMDPException
*/ public String executeQuery(String wsurl, String wsaction,
String functionName, String xmlns, Map ma) throws SMDPException {
try { String ws = getWSRequest(functionName, xmlns, ma); System.out.println("request::::" + ws);
//定义信息资源字符串
URL url = new URL(wsurl);
// 创建读取和写入此URL引用资源的对象
URLConnection conn = url.openConnection(); //设置URL引用资源对象用户隐藏为false
conn.setUseCaches(false);
//设置URL引用资源对象输入为true
conn.setDoInput(true);
//设置URL引用资源对象输出为true
conn.setDoOutput(true);
//设置请求属性
conn.setRequestProperty("Content-Length", Integer.toString(ws
.length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-16"); conn.setRequestProperty("SOAPAction", "\"" + wsaction + "\""); OutputStream os = conn.getOutputStream(); OutputStreamWriter oswriter = new OutputStreamWriter(os, "utf-16"); oswriter.write(ws);//创建一个新的字符流ws
oswriter.flush();//刷新输出流
oswriter.close();//关闭输出流复写
//定义URLConnection的输入流istream
InputStream istream = conn.getInputStream();
//为输入流定义一个输入流读取对象isreader
InputStreamReader isreader = new InputStreamReader(istream, "utf-8");
//为isreader定义一个缓冲读取对象read
BufferedReader read = new BufferedReader(isreader);
//为read定义 读取一行的string类型对象rr
String rr = read.readLine(); istream.close();
isreader.close();
return rr;
} catch (IOException io) {
io.printStackTrace();
return "500";
} catch (Exception e) {
e.printStackTrace();
return "500";
} }
}
-----------------------------------------------------------------
package org.srit.smdp.util.ws;import java.util.List;
import java.io.StringReader;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentException;public class XmlUtil {
/**
* Parser the SOAP return message,shuck off the SOAP envelope
* 解析soap请求语言
* @param wsmessage
* @return retur the message body
*/
public String getSOAPMessageBody(String soapmessage)
{
/***
*这次修运行soap 语言有破坏的性质
*this modify the return soap message have dirty character
**/
String message = soapmessage.substring(0, soapmessage.lastIndexOf(">") +1);
SAXReader saxReader = new SAXReader();
StringReader sr = new StringReader(message);
try
{
Document document = saxReader.read(sr);
return document.getStringValue();
}
catch(DocumentException de)
{
de.printStackTrace();
return "";
}
}
public static void main(String[] args){
XmlUtil xmutil= new XmlUtil();
}
}
--------------
看能否借鉴上,我用的是AJAX写的