java httpclient 返回xml_Java 使用HttpClient发送GET请求,并获得返回的xml信息

该博客介绍了如何在Java中使用HttpClient库发送GET请求到指定URL(如百度搜索),并展示如何通过DOM解析返回的XML数据。示例代码展示了从HTTP响应中获取内容并打印,以及解析XML文件中特定元素(如time和grade)的过程。
摘要由CSDN通过智能技术生成

本文使用HttpClient发送GET请求,并使用DOM方式获得返回的xml信息。(本人刚开始学习Java,有错误望斧正)

1.使用HttpClient发送GET请求

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class SendGetRequest {

public static void main(String[] args) throws IOException {

// 向百度发送GET请求

CloseableHttpClient httpClient = HttpClients.createDefault();

try {

HttpGet httpGet = new HttpGet(

"http://www.baidu.com/s?ie=UTF-8&wd=blog.myriadrecord.com");

CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

try {

System.out.println(httpResponse.getStatusLine());

HttpEntity httpEntity = httpResponse.getEntity();

//添加处理返回数据的代码

System.out.println(EntityUtils.toString(httpEntity));// 例如将返回的数据转换为字符串并输出

EntityUtils.consume(httpEntity);

} finally {

httpResponse.close();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

httpClient.close();

}

}

}

2.解析XML

解析的xml文件如下:

485532

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class sendGetRequsetAndShowXMLInfo {

static String URL = "http://8.8.8.8/services/CommonSession/getStoreTree?storeId=0";

public static void main(String[] args) throws Exception {

HttpClient httpClient = new DefaultHttpClient();

HttpGet httpGet = new HttpGet(URL);

HttpResponse response = httpClient.execute(httpGet);

HttpEntity entity = response.getEntity();

if (null != entity) {

InputStream in = entity.getContent();//将返回的内容流入输入流内

EntityUtils.consume(entity);

httpGet.releaseConnection();

DocumentBuilderFactory factory = DocumentBuilderFactory

.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(in);//用输入流实例化Document

Element rootElement = document.getDocumentElement();

System.out.println(rootElement.getNodeName() + ": name="

+ rootElement.getAttribute("name"));

NodeList timeNode = rootElement.getElementsByTagName("time");

System.out.println(timeNode.item(0).getNodeName() + ":"

+ timeNode.item(0).getTextContent());

NodeList gradeNode = rootElement.getElementsByTagName("grade");

NodeList classNode = gradeNode.item(0).getChildNodes();

for (int i = 0; i < classNode.getLength(); ++i) {

Element e = (Element) classNode.item(i);

System.out.println(e.getNodeName() + ": name="

+ e.getAttribute("name") + " Number="

+ e.getTextContent());

}

}

}

}

注:之所以xml的格式如此之难看是因为本方法无法跳过空白,

2013-9-9

48

55

32

以上格式的xml文件的缩进空白会同样被视作节点,从而使getChildNodes()返回的NodeList包含空白节点,需要添加额外判断。

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值