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();
  }
 }
 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值