如何从Web服务返回XML [英] How to return XML from web service
问题描述
这可能是疯狂/愚蠢/愚蠢/冗长的问题之一,因为我是网络服务的新手。
我想写一个Web服务,它将以XML格式返回答案(我正在使用我的YUI自动完成服务)。我正在使用Eclipse和Axis2并遵循 http://www.softwareagility.gr/index .php?q = node / 21
我希望以下列格式回复
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>
代码
元素的数量可能会有所不同,具体取决于响应。
直到现在我尝试了以下方法:
1)使用String buffer创建XML并返回字符串。(我提供部分代码以避免混淆)
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());
}
它提供以下不需要的< ns的响应: myServiceResponse>
和< ns:return>
元素。
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>
但它与YUI自动完成功能无关(可能是因为它需要上述格式的响应)
2)使用DocumentBuilderFactory :
赞
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);
}
出现以下错误
org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.sun.org.apache.xerces.internal.dom
3)使用servlet:我尝试使用简单的servlet获得相同的响应并且它有效。这是我的servlet
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());
}
它给出了与上面提到的相同的响应,它与YUI自动完成无关标签。
It gave response same as mentioned above and it worked with YUI autocomplete without any extra tag.
请问您能告诉我如何在没有任何不需要的元素的情况下获得XML响应?
Please can you tell how can I get XML response without any unwanted elements ?
谢谢。
推荐答案
最后让它工作,虽然我不是能够删除不需要的元素。 (在所有事情都到位之前我都不打扰)。我使用AXIOM来生成响应。
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);
}
链接: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
这篇关于如何从Web服务返回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!