如何从Java中读取Web服务的结果?

时间:2012-04-20 21:57:45

标签: .net xml web-services jsp servlets

我有一个基于.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。 我怎么读呢?

1 个答案:

答案 0 :(得分:0)

要解决此问题,我将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>");
   %>