我需要在服务器中发出获取请求以获取信息。我要求在 xml 中包含此信息,我的问题是我试图将此答案保留为 xml 对象。我找到了很多答案,但只是为了创建文档,而不仅仅是拥有对象(我需要说我不允许在客户端创建文件)。
我现在有这个代码:
URL url;
String urlForScores = urlToRead+"format=xml";
HttpURLConnection conn = null;
String line;
String result = "";
try {
url = new URL(urlForScores);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/xml");
readerChannel = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
while ((line = readerChannel.readLine()) != null) {
result += line;
}
readerChannel.close();
JAXBContext jc = JAXBContext.newInstance(String.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xmlSource = new StreamSource(new StringReader(result));
JAXBElement<String> je = unmarshaller.unmarshal(xmlSource, String.class);
System.out.println(je.getValue());
} catch (Exception e) {
e.printStackTrace();
}
你能帮我吗 ??
谢谢 !