搜尋
首頁类库下载java类库java解析Response中XML

java解析Response中XML

Oct 09, 2016 am 09:50 AM

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"));
                 }        
             }      
                   
 }
}

最近給一個公眾平台做一個天氣查詢的功能,原理就是從介面取得資料然後回傳給使用者。這裡使用的介面回傳的是xml類型的資料。於是使用dom4j解析。


    本來想著使用Document document = reader.read();

然後他的參數是一個input流,就想著如何把get請求到的resp

然後他的參數是一個input流,就想著如何把get請求到的resp為流,資料上網查了一些相關,發現比較扯,,,自己搞吧... 先從

      HttpGet httpget = new HttpGet(_url);    
        HttpResponse response = httpclient.execute(httpget);    
        HttpEntity entity = response.getEntity();       
            InputStream inputs=entity.getContent();

這裡得到resp的返回流,直接傳給reader.read();方法,發現報錯說是編碼錯誤。 。 。一臉懵逼,因為直接透過瀏覽器存取就是utf-8編碼啊,臥槽什麼鬼。 。 。

後來使用entity.getContentEncoding()   列印發現是返回流是gzip編碼......Ri...


使用java自帶的解壓解一下壓
GZIPInputStream in = new GZIPInputStream(inputs);

本文出自“TheAuroraSec” 博客,請務必保留此出處http://aurorasec.blog.51cto.com/9752323/1859599

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用