1

我正在使用httpclient在java中发布一个xml。我也期待一个xml作为回应。如何将响应解析为 xml?

这是我到目前为止所拥有的

HttpClient cliet = HttpClientFactory.getClient("mhurl.com");
...
PostMethod post;
post.setRquestEntity(new StringRequestEntity(myxml_in_string,"xml",...);
...
int result = client.executeMethod(post);
InputStream in = post.getRespoinseBodyAsStream();
?? //Not sure how to handle the response from here

谢谢。

4

1 回答 1

1

标准 Java DOM API:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(in);

最后一行很关键。

SAX API

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(in, yourCustomCallback);

JAXB

假设您有合适的模型:

JAXBContext context = JAXBContext.newInstance(Response.class);
Response response = (Response)context.createUnmarshaller().unmarshal(in);
于 2012-07-25T19:05:28.973 回答