这可能是疯狂/愚蠢/愚蠢/冗长的问题之一,因为我是网络服务的新手.
我想写一个Web服务,它将以
XML格式返回答案(我正在使用我的服务进行YUI自动完成).我正在使用
Eclipse和Axis2并遵循
http://www.softwareagility.gr/index.php?q=node/21
我希望以下列格式回复
代码元素的数量可能因响应而异.
直到现在我尝试了以下方法
1)使用String buffer创建XML并返回字符串.(我提供部分代码以避免混淆)
public String myService ()
{
// Some other stuff
StringBuffer outputXML = new StringBuffer();
outputXML.append("<?xml version='1.0' standalone='yes'?>");
outputXML.append("");
while(SOME_CONDITION)
{
// Some business logic
outputXML.append(""+"
");
}
outputXML.append("");
return (outputXML.toString());
}
它提供以下响应,包含不需要的< ns:myServiceResponse>和< ns:return>元件.
但它没有使用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("");
while(SOME_CONDITION)
{
// Some business logic
outputXML.append("" + "
");
}
outputXML.append("");
out.println(outputXML.toString());
}
它给出了与上面提到的相同的响应,它与YUI自动完成一起工作,没有任何额外的标记.
请问您能告诉我如何在没有任何不需要的元素的情况下获得XML响应?
谢谢.