axis

package axis2;

import javax.xml.stream.XMLStreamReader;

import model.User;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.util.StreamWrapper;

public class PrograClient
{
    public static OMElement getBody(Object obj, String nameSpace)
    {
       
        XMLStreamReader reader = BeanUtil.getPullParser(obj);
       
        StreamWrapper parser = new StreamWrapper(reader);
       
        StAXOMBuilder staxOMBuilder = OMXMLBuilderFactory.createStAXOMBuilder(
                OMAbstractFactory.getOMFactory(), parser);
       
        OMElement element = staxOMBuilder.getDocumentElement();
       
        OMNamespace ns = OMAbstractFactory.getOMFactory().createOMNamespace(
                nameSpace, "m");//m是变量名,nameSpace是变量值
       
        element.setNamespace(ns);
       
        return element;
    }
   
    public static void main(String[] args)
    {
        //        User u = new User();
        //        u.setAge(23);
        //        u.setName("tian");
        //        System.out.println(getBody(u, "a"));
        ServiceClient sender = null;
        try
        {
            String xml = "<m:String xmlns:m='http://axis2'><bytes>f </bytes><empty>false</empty></m:String>";
           
            Options options = new Options();
            options.setTo(new EndpointReference(
                    "http://127.0.0.1:8888/axis2/services/simpleServer"));
            options.setTransportInProtocol("SOAP");
            options.setAction("urn:simpleMethod");//HSSV900R008C01SPC200#LST_HAUTHINF
            options.setProperty(HTTPConstants.CHUNKED, "false");
            sender = new ServiceClient();
            sender.setOptions(options);
            OMElement body = str2OMElement(xml);//xml必须有命名空间否则解析不出
            //OMElement body = getBody("ddd", "http://axis2");//终于懂了,原来body里只放body以内的节点
            System.out.println(body);
            OMElement result = sender.sendReceive(body);
            System.out.println(result);
            sender.cleanupTransport();
            sender.cleanup();
        }
        catch (Exception axisFault)
        {
            axisFault.printStackTrace();
            try
            {
                if (null != sender)
                {
                    sender.cleanupTransport();
                    sender.cleanup();
                }
            }
            catch (AxisFault e)
            {
                e.printStackTrace();
            }
        }
    }
   
    /**
     * 字符串转换成OMElement
     *
     * @param xmlStr 需转换的字符串
     * @return a
     */
    public static OMElement str2OMElement(String xmlStr)
    {
        OMElement xmlValue;
        try
        {
            xmlValue = new StAXOMBuilder(new ByteArrayInputStream(xmlStr
                    .getBytes("UTF-8"))).getDocumentElement();
            return xmlValue;
        }
        catch (Exception e)
        {
            return null;
        }
    }
}

你可能感兴趣的:(axis2)