首页 类库下载 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转换为流,也上网查了一些相关资料,发现比较扯,,,自己搞吧... 先从

      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.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)