对象转化成xml
问?:
原因是异构平台,在java services端不能返回对象,只能返回xml,然后在vb客户端解析xml,请问在java中怎么将对象转化成xml,以及在vb中怎么解析xml,请高手指点...
答!: 1:
那你可不可以读取对象中的数据构造xml呢?
答!: 2:
用JDOM读对象的属性,并构造XML字符串,并将这个XML字符串输出到JSP页面上。
答!: 3:
我这有一例子:
---------------------------
/**
*
*/
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写的
---------------------------
/**
*
*/
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写的
相关JAVA教程:
初次接触hibernate有些问题不解,急!
学习Java设计模式,大家看了哪些书?怎么学习最快?
再次求求大家了,hibernate问题
问个SWT的问题,希望大家帮忙解决!
各位大大,如何判断客户离开了我的网站...比较棘手
GUI设计中的类型检查
bot.jar包在什么地方可以下载到
javamail:请问怎样获取页面提交的邮件内容的类型即contentType???20分
关于Resin和Tomcat中文乱码的一个非常奇怪的问题
请问如何用SWT做一个可以在硬盘上打开图片文件的程序!
哪位高手帮我解决在EJB里面如何进行字符集转换?
我是毕业的计算机本科生,在工作的选择上犹豫