那你可不可以读取对象中的数据构造xml呢?

解决方案 »

  1.   

    用JDOM读对象的属性,并构造XML字符串,并将这个XML字符串输出到JSP页面上。
      

  2.   

    我这有一例子:
    ---------------------------
    /**
     * 
     */
    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写的