我使用的API以这种格式返回XML:
<response>
<status>OK</status>
<date>2015-11-25T09:15:40.11+01:00</date>
<page>1</page>
</response>
但是当我通过java http发送请求时,我得到HTML元素作为响应:
</plaincontent><htmlcontent><![CDATA[<div style="page-break-before:always; page-break-after:always"><div><div class="block"><p>
在响应中,如何只解析XML?输出div
个元素和CSS(响应很长,可以在这里发布)。
我使用HttpClient
:
String URL = url.substring(1, url.length());
//setting usr + pass Credentials with Realm
client.getState().setCredentials(
new AuthScope(urlAuth, 443, "Realm"),
new UsernamePasswordCredentials("user", "pass")
);
GetMethod get = new GetMethod(URL);
get.setDoAuthentication(true);
try{
int status = client.executeMethod(get);
//System.out.println(status + "\n" + get.getResponseBodyAsString());
//parsing XML from response
System.out.println(get.getResponseBodyAsString());
}finally{
get.releaseConnection();
}