这可能是一个疯狂/愚蠢/愚蠢/冗长的问题,因为我是新手对网络服务。
我想要写一个web服务,它将以XML格式返回答案(我正在使用我的服务用于YUI自动完成)。我使用的是EclipseandAxis2,下面是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>code元素的数量可能因响应而异。直到现在,我尝试了以下方法
( 1)使用字符串缓冲区创建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());
}它用不需要的<ns:myServiceResponse>和<ns:return>元素给出以下响应。
<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:
喜欢
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
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自动完成工作,没有任何额外的标签。
请告诉我如何在没有任何不需要的元素的情况下获得XML响应?
谢谢。
发布于 2011-08-03 13:20:29
虽然我无法删除不想要的元素,但终于开始工作了。(在一切就绪之前,我不会费心)。我使用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);
}链接: 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](http://sv.tomicom.ac.jp/~koba/axis2-1.3/docs/xdocs/1_3/rest-ws.html)发布于 2011-08-03 11:24:04
Axis2用于将对象传递回调用方。这就是为什么它为响应添加了额外的内容,即使它是一个简单的字符串对象。
使用第二种方法,您的服务返回一个复杂的Java (Element实例),用于描述XML片段。这样,调用方必须知道这个对象才能反序列化它,并恢复包含XML数据的Java对象。
对于返回类型,第三种方法是最简单和最好的:它不返回序列化的Java对象,只返回普通的xml文本。当然,您可以使用DocumentBuilder来准备XML,但最终您必须通过调用适当的getXml()、asXml()方法(或某种.)来生成XML字符串。
发布于 2011-08-03 12:07:44
我认为您不能用Axis返回自定义xml。它无论如何都会把它包在信封里。
https://stackoverflow.com/questions/6925546
复制相似问题