How to return XML from web service

22,581

Solution 1

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

Solution 2

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...)

Share:
22,581
Ajinkya
Author by

Ajinkya

So called human, developer, reader, StackOverflower... #1XEngineer :D @LinkedIn

Updated on July 24, 2020

Comments

  • Ajinkya
    Ajinkya almost 4 years

    This may be one of the insane / stupid / dumb / lengthy questions as I am newbie to web services.
    I want to write a web service which will return answer in XML format (I am using my service for YUI autocomplete). I am using Eclipse and Axis2 and following http://www.softwareagility.gr/index.php?q=node/21 I want response in following format

    <codes>
    <code value="Pegfilgrastim"/>
    <code value="Peggs"/>
    <code value="Peggy"/>
    <code value="Peginterferon alfa-2 b"/>
    <code value="Pegram"/>
    </codes>
    

    Number of code elements may vary depending on response. Till now I tried following ways
    1) Create XML using String buffer and return the string.(I am providing partial code to avoid confusion)

    public String myService ()
    {    
        // Some other stuff
        StringBuffer outputXML = new StringBuffer();
        outputXML.append("<?xml version='1.0' standalone='yes'?>");
        outputXML.append("<codes>");
        while(SOME_CONDITION)
        {
           // Some business logic
           outputXML.append("<code value=\""+tempStr+"\">"+"</code>");    
        }
        outputXML.append("</codes>");
        return (outputXML.toString());  
    }
    

    It gives following response with unwanted <ns:myServiceResponse> and <ns:return> element.

    <ns:myServiceResponse>
    <ns:return>
    <?xml version='1.0' standalone='yes'?><codes><code value="Peg-shaped teeth"></code><code value="Pegaspargase"></code><code value="Pegfilgrastim"></code><code value="Peggs"></code><code value="Peggy"></code><code value="Peginterferon alfa-2 b"></code><code value="Pegram"></code></codes>
    </ns:return>
    </ns:findTermsResponse>    
    

    But it didnt work with YUI autocomplete (May be because it required response in format mentioned above)
    2) Using DocumentBuilderFactory :
    Like

    public Element myService ()
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element codes = doc.createElement("codes");
        while(SOME_CONDITION)
        {
          // Some business logic
          Element code = doc.createElement("code");
          code.setAttribute("value", tempStr);
          codes.appendChild(code);
        }
        return(codes);
    }  
    

    Got following error

    org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.sun.org.apache.xerces.internal.dom  
    

    3) Using servlet : I tried to get same response using simple servlet and it worked. Here is my servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
      {
        StringBuffer outputXML = new StringBuffer();
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        outputXML.append("<?xml version='1.0' standalone='yes'?>");
        outputXML.append("<codes>"); 
        while(SOME_CONDITION)
        {
            // Some business logic
            outputXML.append("<code value=\"" + tempStr + "\">" + "</code>");    
        }
        outputXML.append("</codes>");
        out.println(outputXML.toString());
    }
    

    It gave response same as mentioned above and it worked with YUI autocomplete without any extra tag.

    Please can you tell how can I get XML response without any unwanted elements ?

    Thanks.