使用JAXB解析响应XML
问题描述:
我想使用JAXB从非wsdl Web服务调用中读取响应。 我使用HttpURLConnection发送POST请求,并获得响应。 我的问题是我从响应流中创建一个XML文档,然后使用jaxb制作java对象?或者,是否有可能在响应流中随时使用jaxb? 这将是一个web应用程序,我将无法在任何地方存储生成的xml文档,所以如果我需要制作一个xml文档,我如何将它存储为jaxb使用,如果我不能在jaxb上执行飞?使用JAXB解析响应XML
答
下面是一个例子:
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();
更多信息参见: