【问题标题】:How to return XML from web service如何从 Web 服务返回 XML
【发布时间】:2011-08-03 11:12:11
【问题描述】:

这可能是一个疯狂/愚蠢/愚蠢/冗长的问题,因为我是网络服务的新手。
我想编写一个 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)使用字符串缓冲区创建 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());  
}

它使用不需要的&lt;ns:myServiceResponse&gt;&lt;ns:return&gt; 元素给出以下响应。

<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 响应?

谢谢。

【问题讨论】:

    标签: java web-services axis2


    【解决方案1】:

    Axis2 用于将对象传递回调用者。这就是为什么它会在响应中添加额外的东西,即使它是一个简单的 String 对象。

    使用第二种方法,您的服务会返回一个用于描述 XML 片段的复杂 Java 对象(Element 实例)。这样,调用者必须知道这个对象才能反序列化它并恢复包含 XML 数据的 Java 对象。

    第三种方法是关于返回类型的最简单和最好的方法:它不返回序列化的 Java 对象,只返回纯 xml 文本。当然你可以使用DocumentBuilder 来准备XML,但最后你必须通过调用适当的getXml()asXml() 方法(或某种......)来制作它的字符串

    【讨论】:

    • 感谢您的回复。我尝试使用 servlet 来查看问题出在哪里。除了网络服务,我别无选择。如何在 Web 服务中实现相同(就像我在 servlet 中所做的那样)?
    • 据我所知,Web 服务由 servlet 组成。你必须使用Axis2?如果是,那么我认为摆脱 Web 服务实现的“装饰”是不可能的。
    【解决方案2】:

    虽然我无法删除不需要的元素,但终于让它工作了。 (在一切就绪之前我不会打扰)。我使用 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

    【讨论】:

      【解决方案3】:

      我认为您无法使用 Axis 返回自定义 xml。无论如何,它都会将其包裹在信封中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-05
        • 2011-03-01
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        • 1970-01-01
        相关资源
        最近更新 更多