如何在调用特定WebResource后返回xml文件? 我当前的一个字符串返回
WebResource webResource = client.resource("http://api.foursquare.com/v1/venues");
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("geolat", String.valueOf(lattitude));
queryParams.add("geolong", String.valueOf(longitude));
return webResource.queryParams(queryParams).get(String.class);
我后来想用XPath来解析xml,因为它会更容易......有没有办法直接将它检索到.xml或者我是否必须从这个字符串创建一个xml?如果必须的话我该怎么办呢?
答案 0 :(得分:1)
我不确定以下内容是否有效,但值得一试。
变化:
return webResource.queryParams(queryParams).get(String.class);
要:
return webResource.queryParams(queryParams).get(Source.class);
或者,您可以使用java.net API并将结果作为流获取。以下示例取自my blog:
String uri =
"http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
(Customer) jc.createUnmarshaller().unmarshal(xml);
connection.disconnect();