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")); // get the XML root element Element root = document.getrootelement (); // Get all the child nodes of the root element 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")); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Low" + ":" + Weathernode.elementtexttrim ("Low")); iterator weathernodechild = weathernode.elementiterator (" Day "); element daynode = (Element) weathernodechild.next (); system.out.println (); &nbSp; 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")); &nBsp; system.out.println ("Value" + ":" + zhishunode.elementtexttrim ("value")); system.out.println ("Detail" + ":" + zhishunode.elementtexttrim ("detail")); } } }}
Recently to a public platform to do a weather query function, the principle is to get the data from the interface and then return to the user. The interface used here returns data of the XML type. Then use dom4j parsing.
Originally wanted to use document document = Reader.read ();
Then his argument is a input stream, thinking how to get the GET request to the RESP into a stream, but also on the Internet to check some relevant information, found that the comparison,,, do it yourself ... First from
HttpGet httpget = new HttpGet (_url); HttpResponse response = Httpclient.execute (HttpGet); httpentity entity = response.getentity (); InputStream inputs=entity.getcontent ();
Here to get the return stream of resp, directly to Reader.read (), method, found that the error is coding errors ... A face is crazy, because directly through the browser access is Utf-8 code ah, lying trough what ghost ...
Later using entity.getcontentencoding () The print discovery is that the return stream is gzip encoded ... Ri ...
Use Java's own decompression to solve the pressure is good ....
Gzipinputstream in = new Gzipinputstream (inputs);
This article is from the "theaurorasec" blog, make sure to keep this source http://aurorasec.blog.51cto.com/9752323/1859599
Java parsing response in XML