HttpURLConnection + SAXReader解析XML

package com.ott.service.impl;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.xml.sax.InputSource;


import com.ott.Utils.SysUtils;
import com.ott.beans.LiveContent;
import com.ott.service.LiveContentXmlService;


@Service
public class LiveContentXmlServiceImpl implements LiveContentXmlService {


@Override
public List<LiveContent> parse(Date date) {
String httpUrl = SysUtils.Wangsu_URL;
List<LiveContent> contents = new ArrayList<LiveContent>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
StringReader sr = null;
Document document = null;
try{
String logFileUrl = httpUrl+"liveplay/livecontent."+sdf.format(date)+".xml";
    String result = sendGet(logFileUrl);
    if(StringUtils.isNotEmpty(result)){
    SAXReader reader = new SAXReader();
    sr = new StringReader(result);
    InputSource is = new InputSource(sr);
       
           document = reader.read(is);
           Element root = document.getRootElement();
           Element dateElement = root.element("Date");
           List<Element> contentElementList = dateElement.elements("livecontent");
           LiveContent content;
           for(Element e : contentElementList){
            content = new LiveContent();
            content.setId(Integer.parseInt(e.attributeValue("id")));
            content.setLcname(e.attributeValue("lcname"));
            content.setLcimgurl(e.attributeValue("lcimgurl"));
            content.setLtname(e.attributeValue("ltname"));
            content.setLturl(e.attributeValue("lturl"));
            content.setLivecontent(e.attributeValue("livecontent"));
            content.setLivememo(e.attributeValue("livememo"));
            content.setOpentime(e.attributeValue("opentime"));
            content.setEndtime(e.attributeValue("endtime"));
            content.setDuration(Integer.parseInt(e.attributeValue("duration")));
                content.setCreateuser("SYS");
                content.setUpdateuser("SYS");
                contents.add(content);
            }
    }
        } catch (DocumentException e) {
            e.printStackTrace();
        }finally {
        if(sr != null){
        sr.close();
        }
        if(document != null){
        document.clearContent();
        }
}
return contents;
}

/**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url) {
    String result = "";
        try{
        String urlName = "";
        urlName = new String((url).getBytes("UTF-8"),"GBK");
        URL _url = new URL(urlName);
        HttpURLConnection  connection = (HttpURLConnection)_url.openConnection();
        //设置通用的请求属性  
        connection.setRequestProperty("accept", "*/*");   
        connection.setRequestProperty("connection", "Keep-Alive");  
        connection.setRequestProperty("user-agent",   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        //设置连接超时10s
        connection.setConnectTimeout(10*1000);
        //读取数据超时10s
            connection.setReadTimeout(10*1000);
            connection.setUseCaches(false);
            connection.connect();          
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine())!= null){
          result += line;
            }
            in.close(); 
            connection.disconnect();
        }catch(Exception e){
           System.out.println("没有结果!"+e);
        }
        return result;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值