图书馆管理员郑州市:apache axis 解析.netc# wse dataset

来源:百度文库 编辑:偶看新闻 时间:2024/05/10 16:47:41

apache axis 解析.netc# wse dataset

    博客分类:
  • java
ApacheWeb.netXMLDelphipackage etpsmsws.etpsms.hnas;


import java.util.Iterator;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.MessageElement;
import org.apache.axis.types.Schema;

/**
*


* Title:
*


*


* Description:
*


*


* Copyright: Copyright (c) 2004
*


*


* Company:
*


*
* @author not attributable
* @version 1.0
*/
public class testSoap2 {
public testSoap2() {
}

public static void main(String[] args) {
try {

String wsdlUrl = "http://test";
String soapActionURI = "HNAS.EtpSms.EtpSmsWS/GetRecvFromTemp";

Service service = new Service();
Call call = (Call) service.createCall();
//
// call.setOperationName(new QName("HNAS.EtpSms.EtpSmsWS",
// "EtpSmsWSSoap"));
call.setOperationName(new QName("HNAS.EtpSms.EtpSmsWS",
"GetRecvFromTemp"));
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
call.addParameter(new QName("HNAS.EtpSms.EtpSmsWS", "iMemberId"),
org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);

Object[] objs = new Object[] { "8918"};
Object res = call.invoke(objs);
// System.out.println(res);
Schema schema = (Schema) res;
MessageElement[] messageElement = schema.get_any();
for (int i = 0; i < messageElement.length; i++) {
// System.out.println(messageElement[i].getChildElements());
// System.out.println(messageElement[i].getRealElement()
// .getChildren());

Iterator iterator = messageElement[i].getChildElements();
while (iterator.hasNext()) {
MessageElement m = (MessageElement) iterator.next();
m = m.getRealElement();
Iterator it = m.getChildElements();
while (it.hasNext()) {
m = (MessageElement) it.next();
it = m.getChildElements();
while (it.hasNext()) {
m = (MessageElement) it.next();
System.out.println(m.getValue());
}
}
}
}

} catch (Exception ex) {
System.err.println(ex.toString());
}
}
}


注意事项:
参数必须是String类型的,你定义的是啥类型不管。
setOperationName 要给方法名。







1. 概述
  
  很多正在开发或者打算开发XML Web Services的程序员都问过这样的一个问题:"我的Web Service返回的结果是一个DataSet类型的对象,但如果我的客户端不是用.NET写的(因而没有内建的DataSet类型),

那该如何调用这个Web Service并访问DataSet中的数据呢?"。
  
  对于这个问题,首先应该说的是:1)在多种语言共存的编程环境下,是不适合使用类似DataSet这种只属于特定语言的数据类型的。不管是在XML Web Services还是CORBA的环境中,都应该尽量使用简单数据类型以及简单数据类型的数组。2)应当很谨慎的决定是否需要通过Web Service来返回大量数据。由于网络传输的开销既包括HTTP连接建立的时间,也包括传送数据的时间,因此需要在减少访问服务器次数和减少网络传输量之间寻找一个合适的平衡。如非必须,则不适合通过Web Service传送含有几十条或者几百条数据的数据表。
  
  然后,就问题本身而言,.NET Web Services返回的DataSet类型是可以直接被其他非.NET的客户端解析的,因为即便是DataSet类型的返回值,也会被表达成XML格式再进行传输。下面的例子就是一个返回类型为DataSet的Web Method,及其被调用后返回的XML格式数据:

2. 创建.NET Web Services,返回数据集合
    [WebMethod]
    public DataSet GetPersonTable(string str)
    ...{
        DataTable table = new DataTable("Person");
        table.Columns.Add("Name");
        table.Columns.Add("Gender");
        table.Rows.Add(new string[2] ...{ "Alice", "Female" });
        table.Rows.Add(new string[2] ...{ "Bob", "Male" });
        table.Rows.Add(new string[2] ...{ "Chris", "Female" });
        table.Rows.Add(new string[2] ...{ "Dennis", "Male" });
        table.Rows.Add(new string[2] ...{ "Eric", "Male" });

        DataSet dataset = new DataSet("PersonTable");
        dataset.Tables.Add(table);

        return dataset;
    }

3. 在Java中调用.NET Web Services,处理返回的数据集合
try ...{

            String wsdlUrl = "http://localhost/WebSite1/Service.asmx?op=GetPersonTable";  
            String soapActionURI = "http://tempuri.org/GetPersonTable";  

            Service service = new Service();  
            Call call = (Call) service.createCall();   
            // 
            call.setOperationName(new QName("http://tempuri.org/","GetPersonTable"));
            call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
            call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_STRING,  
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);  
            call.setUseSOAPAction(true);  
            call.setSOAPActionURI(soapActionURI);
           
            Object[] objs = new Object[]...{"ssss"};
            Object res = call.invoke( objs );
            System.out.println(res);
            Schema schema = (Schema)res;
                    
            DefaultTableModel model=new  DefaultTableModel(new String[]...{"name","gender"},0);
            schema.get_any()[1].getChildNodes().getLength();
            int nLength=schema.get_any()[1].getChildNodes().item(0).getChildNodes().getLength();
            String name="N/A";
            String gender="N/A";
            for(int i=0;i            ...{
                if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getNodeName().equals("Name"))
                ...{
                    name=schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getFirstChild().getNodeValue();
                }
                if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(1).getNodeName().equals("Gender"))
                ...{
                    gender=schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(1).getFirstChild().getNodeValue();
                }   
                model.addRow(new String[]...{name,gender});
                this.jScrollPane1.getViewport().add(jTable1, null);
            }
            jTable1.setModel(model);       
      }
      catch (Exception ex)
      ...{            
          System.err.println(ex.toString());
      }             



4. 小结
  
  从前面的叙述和代码中可以看出,对于"如何在Java/Delphi中使用.NET的Web Service返回的DataSet"的问题,虽然在非.NET语言环境中直接接受DataSet类型的返回值比较困难,但可以有其他的解决方案。