通过XMLReader 读取服务器的xml 文件(通过sax2)

来源:互联网 发布:胸卡 名片制作软件 编辑:程序博客网 时间:2024/06/06 03:05

本文通过参考http://www.iteye.com/topic/763895 网站

通过读本地磁盘取得xml 可以参考  http://www.iteye.com/topic/763895网站       InputStream input = new FileInputStream(new File("D:\\test\\test.xml"));

有三类:TestRssTesteSax2.java| SaxRssTestService.java|RssItemDTO.java

第一类:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class TestRssTesteSax2 {

    public TestRssTesteSax2() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String Url = "http://blog.hiredmyway.com/feed";// Url 就是读这个网站的信息
        SaxRssTestService sax = new SaxRssTestService();
        
         try {
                
                URL u = new URL(Url);
                URLConnection UC = u.openConnection();
                /*
                 * If we don't set the user-agent property sites like Google won't
                 * let you access their feeds.
                 */
                UC.setRequestProperty("User-agent", "www.hiredmyway.com");
                InputStreamReader r = new InputStreamReader(UC.getInputStream());
                
                List<RssItemDTO> blogs = sax.getBlogs(r);
                for(RssItemDTO dto : blogs){
                    System.out.println(dto.toString());
                    System.out.println("-------------------------------");
                }

                System.out.println("count="+blogs.size());
            } catch (Exception e) {
                
            }
        

    }

}
-----------------------------------------------------------------

第二类


import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;



public class SaxRssTestService extends DefaultHandler{


    private List<RssItemDTO> blogs = null;
    private RssItemDTO blog = null;

    private String preTag = null;//作用是记录解析时的上一个节点名称

    public List<RssItemDTO> getBlogs(InputStreamReader xmlStream) throws Exception{
        SAXParserFactory factory = SAXParserFactory.newInstance();
//        SAXParser parser = factory.newSAXParser();
        SaxRssTestService handler = new SaxRssTestService();
        XMLReader xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);
//        xr.parse(xmlStream, handler);
        xr.parse(new InputSource(xmlStream));
        return handler.getBlogs();
    }
    
    public List<RssItemDTO> getBlogs() {
        return blogs;
    }

    public void setBlogs(List<RssItemDTO> blogs) {
        this.blogs = blogs;
    }
    
    @Override
    public void startDocument() throws SAXException {
        blogs = new ArrayList<RssItemDTO>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if("item".equals(qName)){
            blog = new RssItemDTO();
//            blog.setId(Integer.valueOf(attributes.getValue(0)));
        }
        preTag = qName;//将正在解析的节点名称赋给preTag 

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if("item".equals(qName)){
            blogs.add(blog);
            blog = null;
        }
        preTag = null;
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        
        if (blog !=null) {
            if(preTag!=null){
                String content = "";
                for (int i = start; i < start + length; i++) {
                    content += ch[i];
                }
            
                if("title".equals(preTag)){
                    blog.setTitle(content);
                }else if("pubDate".equals(preTag)){
                    blog.setPubDate(content);
                }else if ("link".equals(preTag)) {
                    //description
                    blog.setURL(content);
                } else if ("description".equals(preTag)){
                    if (content !=null && !content.equals("\n")) {
                        blog.setDescription(content);
                    }
                    
                } else if ("content:encoded".equals(preTag)) {
                    if (content !=null && !content.equals("\n")) {
                        blog.setImageUrl(content);
                    }
                    
                }
            }
        }

    }
    
}

====================================

第三个类:

package com.hiredmyway.index.sax2test;

import org.apache.commons.lang.StringUtils;

import com.hiredmyway.util.string.ShortTextSubString;
import com.hiredmyway.util.string.TitleSubString;

public class RssItemDTO {    
        private String Title = "";
        private String URL = "";        
        private String description="";
        private String pubDate="";
        private String imageUrl = "";
        private int id;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getDescription() {
            if(StringUtils.isNotBlank(description)){
                description = new ShortTextSubString().trimSubString(description);
            }
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public void setTitle(String Title) {
            if(StringUtils.isNotBlank(this.Title)){
                this.Title = new TitleSubString().trimSubString(this.Title);
            }
            this.Title = Title;
        }        
        public void setURL(String URL) {
            this.URL = URL;
        }
        
        public String toString() {            
            return this.Title+":"+this.pubDate+":"+this.URL+":"+this.description+":"+this.getImageUrl();
        }
        public String getTitle() {
            return Title;
        }
        public String getURL() {
            return URL;
        }
        public String getPubDate() {
            return pubDate;
        }
        public void setPubDate(String pubDate) {
            this.pubDate = pubDate;
        }
        
        public String getImageUrl() {
            return imageUrl;
        }
        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

}


原创粉丝点击