How can I return a xml file after calling a particular开发者_运维知识库 WebResource? My current one returns as a string
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);
I later want to use XPath to parse the xml as it would be easier... is there a way to retrieve it directly to a .xml or do I have to create a xml from this string? If I have to then how can I do it?
I'm not sure if the following would work, but it may be worth a try.
Change:
return webResource.queryParams(queryParams).get(String.class);
To:
return webResource.queryParams(queryParams).get(Source.class);
Alternatively you could use the java.net APIs and get the result as a stream. The following example is taken from 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();
精彩评论