【问题标题】:How to read result from web service in Java?如何从 Java 中的 Web 服务读取结果?
【发布时间】:2012-04-20 21:57:44
【问题描述】:

我有一个基于 .NET 的 Web 服务,它返回 XmlDocument。

我正在使用这样的 servlet 在 JSP 文件中读取它:

CuteServlet servlet = new CuteServlet();
result = servlet.searchProperties(market_type, location, bedroom, price);

CuteServlet 中的 searchProperties 方法:

public SearchPropertiesResult searchProperties(String propertyMarketType,
     String location, String noOfBedroom, String price) 
{
  wsClient.Property service = new wsClient.Property();
  wsClient.PropertySoap port = service.getPropertySoap();
  return port.searchProperties(propertyMarketType, location, noOfBedroom, price);
}

searchProperties() 的返回类型是 SearchPropertiesResult。 如何阅读?

【问题讨论】:

  • 只是阅读 javadoc 或探索该类的方法?该类不属于标准 API,因此没有人能够详细回答。
  • @BalusC,感谢您的回复。我已经解决了。稍后我会更新答案,以防其他人遇到同样的问题。

标签: .net xml web-services jsp servlets


【解决方案1】:

为了解决这个问题,我将XmlDocument(从C#返回)读取为result并将值放入List<Object>,然后将每个对象转换为Element以读取如下详细信息:

   <% 
   ...
   wsClient.SearchPropertiesResponse.SearchPropertiesResult result 
             = servlet.searchProperties(market_type, location, bedroom, price);
   List<Object> objects = result.getContent();

   Element rootElement = (Element)objects.get(0);
   //out.println(element.getElementsByTagName("property").getLength());
   NodeList propertyList = rootElement.getElementsByTagName("property");
   out.println("<table>");
   int propertiesLength = propertyList.getLength();
   for(int i=0; i<propertiesLength; i++)
   {
   Element aProperty = (Element)propertyList.item(i);
   NodeList childNodeList = aProperty.getChildNodes();

    Element property_idElement = (Element)childNodeList.item(0);
    String property_id = property_idElement.getTextContent();

    Element street_nameElement = (Element)childNodeList.item(2);
    String street_name = (street_nameElement.getTextContent());

    ...
    String tr = "<tr><td>";
    int l = i+1;//"+l+"
    tr += "<b> </b>"+door_no+" "+street_name+", "+post+"<br/>";
    tr += description+"<br/>";
    tr += "Bedroom: "+no_of_bed+"<br/>";
    tr += "Bathroom: "+no_of_bath+"<br/>";
    ...
    tr += "Area: "+area+"<br/><br/><br/><br/>";
    tr += "</td></tr>";
    out.println(tr);
   }
   out.println("</table>");
   %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2013-07-29
    相关资源
    最近更新 更多