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
这可能是疯狂/愚蠢/愚蠢/冗长的问题之一,因为我是网络服务的新手。我想写一个Web服务,它将以XML格式返回答案(我正在使用我的服务进行YUI自动完成)。我正在使用Eclipse和Axis2并关注http://www.softwareagility.gr/index.php?q=node/21我希望以下列格式回复
<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)
代码元素的数量可能因响应而异。直到现在我尝试了以下方法:1)使用String缓冲区创建XML并返回字符串。(我提供部分代码以避免混淆)
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
但它没有使用YUI自动完成(可能是因为它需要上面提到的格式的响应)2)使用DocumentBuilderFactory:喜欢
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
3)使用servlet:我尝试使用简单的servlet获得相同的响应,并且它有效。这是我的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.
它给出了与上面提到的相同的响应,它与YUI自动完成一起工作,没有任何额外的标记。
Please can you tell how can I get XML response without any unwanted elements ?
请问您能告诉我如何在没有任何不需要的元素的情况下获得XML响应?
Thanks.
3 个解决方案
#1
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.
Axis2用于将对象传递回调用者。这就是为什么它为响应添加额外的东西,即使它是一个简单的String对象。
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.
使用第二种方法,您的服务返回一个用于描述XML片段的复杂Java对象(Element实例)。这样调用者必须知道该对象能够对其进行反序列化并恢复包含XML数据的Java对象。
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...)
第三种方法是关于返回类型的最简单和最好的方法:它不返回序列化的Java对象,只返回纯xml文本。当然你可以使用DocumentBuilder来准备XML,但最后你必须通过调用相应的getXml(),asXml()方法(或者......的类型)来创建它的String。
#2
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.
最后让它工作,虽然我无法删除不需要的元素。 (在所有事情都到位之前我都不打扰)。我使用AXIOM来生成响应。
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
链接: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
0
I think you cannot return your custom xml with Axis. It will wrap it into its envelope anyways.
我想你不能用Axis返回你的自定义xml。无论如何它会把它包裹在信封里。
#1
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.
Axis2用于将对象传递回调用者。这就是为什么它为响应添加额外的东西,即使它是一个简单的String对象。
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.
使用第二种方法,您的服务返回一个用于描述XML片段的复杂Java对象(Element实例)。这样调用者必须知道该对象能够对其进行反序列化并恢复包含XML数据的Java对象。
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...)
第三种方法是关于返回类型的最简单和最好的方法:它不返回序列化的Java对象,只返回纯xml文本。当然你可以使用DocumentBuilder来准备XML,但最后你必须通过调用相应的getXml(),asXml()方法(或者......的类型)来创建它的String。
#2
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.
最后让它工作,虽然我无法删除不需要的元素。 (在所有事情都到位之前我都不打扰)。我使用AXIOM来生成响应。
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
链接: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
0
I think you cannot return your custom xml with Axis. It will wrap it into its envelope anyways.
我想你不能用Axis返回你的自定义xml。无论如何它会把它包裹在信封里。