java处理.net webService传回的DataSet对象



总的流程是:

1、取得.Net WebService传过来的DataSet对象;

2、将DataSet对象用schema接收

3、把这个schema对象写入到一个xml文件中

4、解析xml文件,并把解析出来的数据封装成java里的entity

 

package webSerivce;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;  
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import javax.xml.namespace.QName;  
import javax.xml.rpc.ParameterMode;  
import javax.xml.rpc.ServiceException;  
import javax.xml.soap.SOAPException;  
 
import org.apache.axis.client.Call;  
import org.apache.axis.client.Service;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.Schema;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.entity.CardInfo;

/**
 * 测试GetHotelInfoList
 */
public class GetCardInfoTest2 {
 
 private String endpoint = "http://113.57.135.78/DataEngine/wsDataEngineForWebsite.asmx?wsdl";
    
 public CardInfo getCardInfo() throws MalformedURLException,
     ServiceException, RemoteException{
  
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new java.net.URL(endpoint));
        call.setUseSOAPAction(true);
        call.setSOAPActionURI("http://tempuri.org/GetCardInfo");
        call.setOperationName(new QName("http://tempuri.org/","GetCardInfo"));
        call.addParameter(new QName("http://tempuri.org/","CardNo"),                 org.apache.axis.encoding.XMLType.XSD_STRING,ParameterMode.IN);
        call.addParameter(new QName("http://tempuri.org/","Password"), org.apache.axis.encoding.XMLType.XSD_STRING,ParameterMode.IN);
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);//在这里设置webService返回的数据类型
        SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("http://tempuri.org/", "WSAuthHeader");  
        soapHeaderElement.setNamespaceURI("http://tempuri.org/");
       
        try{  
            soapHeaderElement.addChildElement("UserName").setValue("admin");
            soapHeaderElement.addChildElement("Password").setValue("123");
            call.addHeader(soapHeaderElement);
        } catch (SOAPException e) {  
            e.printStackTrace();  
        }
       
        call.addHeader(soapHeaderElement); 
        Object res = (Object) call.invoke(new String[]{"1234","1234"});
        Schema schema = (Schema)res;
        MessageElement[] msgele = schema.get_any();
        List FOCElementHead = msgele[0].getChildren();//消息头,DataSet对象
        List FOCElementBody = msgele[1].getChildren();//消息体信息,DataSet对象
       
        if (FOCElementBody.size() <= 0){
         System.out.println("无消息体");
        }
       
        String nn = FOCElementBody.get(0).toString();//消息体的字符串形式
        try {
            this.saveXMLString(nn,"c://test.xml");//保存为XML形式
            this.parserXml("c://test.xml");
        } catch (IOException e) {
            e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 把读出来的xml数据写成文件,以便下一步解析的时候用
  */
 private void saveXMLString(String XMLString, String fileName)throws IOException {   
        File file = new File(fileName);   
        if (file.exists()) {   
            file.delete();   
        }   
        file.createNewFile();   
        if (file.canWrite()) {   
            FileWriter fileOut = new FileWriter(file);   
            fileOut.write(XMLString);   
            fileOut.close();   
        }   
 }
 
 /**
  * 解析xml文件
  */
 private void parserXml(String fileName) throws MalformedURLException {
  CardInfo cardInfo = new CardInfo();
  File inputXml = new File(fileName);
  SAXReader saxReader = new SAXReader();
  try {
   Document document = saxReader.read(inputXml);
   Element node1 = document.getRootElement();
   List<String> param = new ArrayList<String>();
   

   //遍历xml文件
   for (Iterator i = node1.elementIterator(); i.hasNext();) {
    Element node2 = (Element) i.next();
    
    Iterator node =  node2.elementIterator("no");
    Element ele = (Element) node.next();
    
    for (Iterator j = node2.elementIterator(); j.hasNext();) {
     Element node3 = (Element) j.next();

     System.out.println(node3.getName() + ":" + node3.getText());
     
     param.add(node3.getText());//把元素装入一个数组
     
    }
   }
   
   //在这里   将param数组里的元素封装到entity里
   
  } catch (DocumentException e) {
   System.out.println(e.getMessage());
  }
 }
  
 public static void main(String[] args){
  GetCardInfoTest2 test = new GetCardInfoTest2();
  try {
   test.getCardInfo();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (RemoteException e) {
   e.printStackTrace();
  } catch (ServiceException e) {
   e.printStackTrace();
  }
 }
 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Web服务是一种在网络上进行通信的技术,它使用一个称为SOAP(简单对象访问协议)的协议来传输数据。使用.NET Web服务时,我们可以通过自定义对象参数来传递数据。 自定义对象参数是指我们可以定义自己的类,在Web服务方法中使用这些类作为参数传递给其他应用程序。在定义自定义对象参数时,我们可以添加属性和方法来表示特定的数据和操作。 在.NET Web服务中传递自定义对象参数时,我们需要进行以下几个步骤: 1. 定义自定义对象类:我们首先需要定义自定义对象类,这个类包含我们需要传递的数据和相关操作的属性和方法。 2. 定义Web服务方法:我们需要在Web服务中定义一个方法,这个方法接受自定义对象作为参数,并且执行相关操作。在方法内部,我们可以通过访问自定义对象的属性和方法来操作传递的数据。 3. 调用Web服务方法:在应用程序中,我们可以通过创建Web服务代理来调用Web服务方法。我们可以使用代理对象来传递自定义对象参数,并且调用相关方法。 使用自定义对象参数可以使我们的Web服务更具灵活性和扩展性。通过传递自定义对象,我们可以将相关的数据和操作封装在一起,使得方法调用更简洁和可读性更好。 总之,.NET Web服务提供了一种方便的方式来在网络上进行通信。通过使用自定义对象参数,我们可以传递包含特定数据和操作的自定义对象,使得Web服务更加灵活和可扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值