How to return XML from web service

只愿长相守 提交于 2019-12-04 09:35:33

Axis2 is for delivering Objects back to the caller. Thats why it adds extra stuff to the response even it is a simple String object.

Using the second approach your service returns a complex Java object (Element instance) that is for describing an XML fragment. This way the caller has to be aware of this object to be able to deserialize it and restore the Java object that contains XML data.

The third approach is the simplest and best in your case regarding the return type: it doesn't return a serialized Java object, only the plain xml text. Of course you could use DocumentBuilder to prepare the XML, but in the end you have to make String of it by calling the appropriate getXml(), asXml() method (or kind of...)

Finally got it work though I am not able to remove unwanted element. (I don't bother till all things are in place). I used AXIOM to generate response.

public OMElement myService ()
{
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("", "");
    OMElement codes = fac.createOMElement("codes", omNs);
    while(SOME_CONDITION)
    {
       OMElement code = fac.createOMElement("code", null, codes);
       OMAttribute value = fac.createOMAttribute("value", null, tempStr);
       code.addAttribute(value);
    }
    return(codes); 
}

Links : 1) http://songcuulong.com/public/html/webservice/create_ws.html
2) http://sv.tomicom.ac.jp/~koba/axis2-1.3/docs/xdocs/1_3/rest-ws.html

I think you cannot return your custom xml with Axis. It will wrap it into its envelope anyways.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!