![](/img/trans.png)
[英]how to remove <return> tag from Axis2 web service response in JSON or XML?
[英]How to return XML from web service
這可能是瘋狂/愚蠢/愚蠢/冗長的問題之一,因為我是網絡服務的新手。
我想寫一個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>
code
元素的數量可能因響應而異。 直到現在我嘗試了以下方法
1)使用String buffer創建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響應?
謝謝。
Axis2用於將對象傳遞回調用者。 這就是為什么它為響應添加額外的東西,即使它是一個簡單的String對象。
使用第二種方法,您的服務返回一個復雜的Java對象( Element
實例),用於描述XML片段。 這樣調用者必須知道該對象能夠對其進行反序列化並恢復包含XML數據的Java對象。
第三種方法是關於返回類型的最簡單和最好的方法:它不返回序列化的Java對象,只返回純xml文本。 當然你可以使用DocumentBuilder
來准備XML,但最后你必須通過調用相應的getXml()
, asXml()
方法(或者......的類型asXml()
來創建它的String。
最后讓它工作,雖然我無法刪除不需要的元素。 (在所有事情都到位之前我都不打擾)。 我使用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
我想你不能用Axis返回你的自定義xml。 無論如何它會把它包裹在信封里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.