package org.main; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Demo { public static GZIPInputStream getReponse(String _url) throws ClientProtocolException, IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(_url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream inputs=entity.getContent(); GZIPInputStream in = new GZIPInputStream(inputs); return in; } @SuppressWarnings("rawtypes") public static void main(String[] args) throws Exception { Map<String, String> map = new HashMap<String, String>(); SAXReader reader = new SAXReader(); Document document = reader.read(getReponse("http://wthrcdn.etouch.cn/WeatherApi?city=%E9%95%BF%E6%98%A5")); // 得到xml根元素 Element root = document.getRootElement(); // 得到根元素的所有子节点 List<Element> elementList = root.elements(); Iterator forecast = root.elementIterator("forecast"); if (forecast.hasNext()) { Element recordEless = (Element) forecast.next(); Iterator weather = recordEless.elementIterator("weather"); Element weatherNode = (Element) weather.next(); System.out.println(); System.out.println("date" + ":" + weatherNode.elementTextTrim("date")); System.out.println("high" + ":" + weatherNode.elementTextTrim("high")); System.out.println("low" + ":" + weatherNode.elementTextTrim("low")); Iterator weatherNodeChild = weatherNode.elementIterator("day"); Element dayNode = (Element) weatherNodeChild.next(); System.out.println(); System.out.println("type" + ":" + dayNode.elementTextTrim("type")); System.out.println("fengxiang" + ":" + dayNode.elementTextTrim("fengxiang")); System.out.println("fengli" + ":" + dayNode.elementTextTrim("fengli")); } Iterator zhishus = root.elementIterator("zhishus"); while(zhishus.hasNext()) { Element zhishusNode = (Element) zhishus.next(); Iterator zhishu = zhishusNode.elementIterator("zhishu"); while(zhishu.hasNext()) { Element zhishuNode = (Element) zhishu.next(); System.out.println(); System.out.println("name" + ":" + zhishuNode.elementTextTrim("name")); System.out.println("value" + ":" + zhishuNode.elementTextTrim("value")); System.out.println("detail" + ":" + zhishuNode.elementTextTrim("detail")); } } } }
Recently built a weather query function for a public platform. The principle is to obtain data from the interface and return it to the user. The interface used here returns xml type data. So use dom4j to parse.
I originally thought about using Document document = reader.read();
Then its parameter is an input stream, so I was thinking about how to convert the resp requested by get into a stream, and also checked some related information online. , I found it was a bit ridiculous,,, do it yourself... First get the resp return stream from
HttpGet httpget = new HttpGet(_url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream inputs=entity.getContent();
, and pass it directly to the reader.read(); method, and found that the error was reported as a coding error. . . I was confused, because accessing it directly through the browser is UTF-8 encoding, what the hell. . .
Later, I used entity.getContentEncoding() to print and found that the return stream is gzip encoded...Ri...
Just use the decompression that comes with java to decompress it...
GZIPInputStream in = new GZIPInputStream(inputs);
This article comes from "TheAuroraSec" blog, please be sure to keep this source http://aurorasec.blog.51cto.com/9752323/1859599