title: How to return XML from web service
body: <p>This may be one of the insane / stupid / dumb / lengthy questions as I am newbie to web services.<br>
  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 <a href="http://www.softwareagility.gr/index.php?q=node/21" rel="nofollow">http://www.softwareagility.gr/index.php?q=node/21</a>
  I want response in following format    </p>
  
  <pre><code>&lt;codes&gt;
  &lt;code value="Pegfilgrastim"/&gt;
  &lt;code value="Peggs"/&gt;
  &lt;code value="Peggy"/&gt;
  &lt;code value="Peginterferon alfa-2 b"/&gt;
  &lt;code value="Pegram"/&gt;
  &lt;/codes&gt;
  </code></pre>
  
  <p>Number of <code>code</code> elements may vary depending on response. 
  Till now I tried following ways<br>
  <strong>1) Create XML using String buffer and return the string.</strong>(I am providing partial code to avoid confusion)  </p>
  
  <pre><code>public String myService ()
  {    
      // Some other stuff
      StringBuffer outputXML = new StringBuffer();
      outputXML.append("&lt;?xml version='1.0' standalone='yes'?&gt;");
      outputXML.append("&lt;codes&gt;");
      while(SOME_CONDITION)
      {
         // Some business logic
         outputXML.append("&lt;code value=\""+tempStr+"\"&gt;"+"&lt;/code&gt;");    
      }
      outputXML.append("&lt;/codes&gt;");
      return (outputXML.toString());  
  }
  </code></pre>
  
  <p>It gives following response with unwanted <code>&lt;ns:myServiceResponse&gt;</code> and <code>&lt;ns:return&gt;</code> element.</p>
  
  <pre><code>&lt;ns:myServiceResponse&gt;
  &lt;ns:return&gt;
  &lt;?xml version='1.0' standalone='yes'?&gt;&lt;codes&gt;&lt;code value="Peg-shaped teeth"&gt;&lt;/code&gt;&lt;code value="Pegaspargase"&gt;&lt;/code&gt;&lt;code value="Pegfilgrastim"&gt;&lt;/code&gt;&lt;code value="Peggs"&gt;&lt;/code&gt;&lt;code value="Peggy"&gt;&lt;/code&gt;&lt;code value="Peginterferon alfa-2 b"&gt;&lt;/code&gt;&lt;code value="Pegram"&gt;&lt;/code&gt;&lt;/codes&gt;
  &lt;/ns:return&gt;
  &lt;/ns:findTermsResponse&gt;    
  </code></pre>
  
  <p>But it didnt work with YUI autocomplete (May be because it required response in format mentioned above)<br>
  <strong>2) Using DocumentBuilderFactory</strong> :<br>
  Like   </p>
  
  <pre><code>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);
  }  
  </code></pre>
  
  <p>Got following error   </p>
  
  <pre><code>org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.sun.org.apache.xerces.internal.dom  
  </code></pre>
  
  <p><strong>3) Using servlet :</strong> I tried to get same response using simple servlet and it worked. Here is my servlet   </p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
    {
      StringBuffer outputXML = new StringBuffer();
      response.setContentType("text/xml");
      PrintWriter out = response.getWriter();
      outputXML.append("&lt;?xml version='1.0' standalone='yes'?&gt;");
      outputXML.append("&lt;codes&gt;"); 
      while(SOME_CONDITION)
      {
          // Some business logic
          outputXML.append("&lt;code value=\"" + tempStr + "\"&gt;" + "&lt;/code&gt;");    
      }
      outputXML.append("&lt;/codes&gt;");
      out.println(outputXML.toString());
  }
  </code></pre>
  
  <p>It gave response same as mentioned above and it worked with YUI autocomplete without any extra tag.  </p>
  
  <p>Please can you tell how can I get XML response without any unwanted elements ?  </p>
  
  <p>Thanks.</p>