How to return XML from web service

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 06:03:42

问题


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.


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/6925546/how-to-return-xml-from-web-service

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