web接口返回xml格式数据的处理

最近想做个网站评论者IP归属地查询的东西,调用了 http://webservice.webxml.com.cn/的数据,使用的是: http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp?theIpAddress=218.75.149.133这个接口但是他返回的是: <?xml version="1.0" encoding="utf-8" ?> - <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">  <string>218.75.149.133</string>   <string>湖南省常德市石门县 电信</string>   </ArrayOfString>我现在想他直接输出到网页时显示:湖南省常德市石门县 或者 湖南省常德市石门县,请问如何实现?
2026年09月19日 20:10
有2个网友回答
网友(1):

写个大概,上班再给你看看。
1、获取接口返回信息
StringBuffer sb = new StringBuffer("你的请求地址加参数");
URL url = new URL(sb.toString()); // 创建url对象

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 打开url连接
connection.setRequestMethod("POST"); // 设置url请求方式 ‘get’ 或者 ‘post’
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); // 发送
// 返回发送结果
String inputline = "";
String tempStr = in.readLine();
while (tempStr != null) {
inputline = inputline + tempStr;
tempStr = in.readLine();
}
in.close();
return inputline;
2、解析接口信息(上班再些)

网友(2):

httpClient 抓取这个url地址的响应内容。取到xml字符串后,用dom4j解析