これは、私がWebサービスを初心者にしているため、非常識な/愚かな/愚かな/長い質問であるかもしれません。
答えをXML形式で返すWebサービスを作成したい(私はYUIの自動完成のために私のサービスを使用している)。私は、EclipseとのAxis2を使用して、私はcode
要素の形式WebサービスからXMLを返す方法
<codes>
<code value="Pegfilgrastim"/>
<code value="Peggs"/>
<code value="Peggy"/>
<code value="Peginterferon alfa-2 b"/>
<code value="Pegram"/>
</codes>
数応答によって異なる場合があり、次のレスポンスをしたいhttp://www.softwareagility.gr/index.php?q=node/21 を次しています。 これまでは次のようにしました。
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());
}
それは、不要な<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>
しかし、それはのDocumentBuilderFactoryを使用したYUIのオートコンプリート(それは上記の形式の応答を必要とするので可能性がある)
2)で動作didntは:
ガット
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)サーブレットの使用:シンプルなサーブレットを使って同じレスポンスを得ようとしましたが、うまくいきました。ここに私のサーブレットは
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レスポンスを取得する方法を教えてください。
ありがとうございました。
感謝。私は問題がどこにあるかを知るためにサーブレットを試しました。 Webサービス以外のオプションはありません。どのように(私はサーブレットでdoignですか?)Webサービスで同じ達成することができますか? – xyz
私の知る限り、Webサービスはサーブレットで構成されています。 Axis2を使う必要がありますか?もしそうなら、私はWebサービス実装の "装飾"を取り除くことは不可能だと思います。 – jabal