梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
调用方式1:
package client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import services.IReaderService;
import beans.Reader;

public class ReaderClient
{
    public static void main(String[] args)
    {
        // 这里是创建一个service,需要传入一个接口类,因为我们后面必须调用相应的接口方法
        Service srcModel = new ObjectServiceFactory()
                .create(IReaderService.class);
        // 代理工厂,这里是为了后面创建相应的接口类
        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
                .newInstance().getXFire());
        // webservice地址,不需要加wsdl
        String readerServiceUrl = "http://127.0.0.1:8080/myWebservice/services/readerService";
        
        try
        {
            // 利用工厂返回相应的接口类
            IReaderService readerService = (IReaderService) factory.create(
                    srcModel, readerServiceUrl);
            
            //String str = readerService.sayHello("惠万鹏");
            
//System.out.println(str);
            
            Reader reader = readerService.getReader("shun", "123");
            System.out.println(reader);
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
    }
}
调用方式2:
package client;

import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReaderClient2
{
    public static void main(String[] args)
    {
        Client client = null;
        try
        {
            client = new Client(
                    new URL(
                            "http://127.0.0.1:8080/myWebservice/services/readerService?wsdl"));
            Object[] result1 = client.invoke("sayHello", new Object[] { "张三" });
            System.out.println(result1[0]);
            
            //Object[] result2 = client.invoke("getReader", new Object[] { "张三", "123456" });
            Object[] result2 = client.invoke("getReaders", new Object[] { null });
            Document xmlTree = (Document) result2[0];
            System.out.println("x: " + xmlTree.toString());
            
            anylizeElement(xmlTree);
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private static void anylizeElement(Document xmlTree) {
        Element element = xmlTree.getDocumentElement();
        System.out.println(element.getFirstChild().getFirstChild()
                .getNodeName());
        System.out.println(element.getFirstChild().getFirstChild()
                .getNodeValue());
        NodeList children = element.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            stepThrough(node);
        }
    }
     
    private static void stepThrough(Node start) {
        for (Node child = start.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child instanceof Node)// 去除多余的空白
            {
                System.out.print("节点名:" + child.getNodeName());
                System.out.println("\t节点值:" + child.getNodeValue());
            }
            if (child != null)
                stepThrough(child);
        }
    }
    
}
posted on 2014-07-06 14:40 HUIKK 阅读(239) 评论(0)  编辑  收藏 所属分类: webservice

只有注册用户登录后才能发表评论。


网站导航: