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;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值