자바

http://fs.jtbc.joins.com/RSS/newsflash.xml
http://fs.jtbc.joins.com/RSS/politics.xml
http://fs.jtbc.joins.com/RSS/economy.xml
http://fs.jtbc.joins.com/RSS/society.xml
http://fs.jtbc.joins.com/RSS/international.xml
http://fs.jtbc.joins.com/RSS/culture.xml
http://fs.jtbc.joins.com/RSS/entertainment.xml
http://fs.jtbc.joins.com/RSS/sports.xml
http://fs.jtbc.joins.com/RSS/fullvideo.xml
http://fs.jtbc.joins.com/RSS/newsrank.xml
http://fs.jtbc.joins.com/RSS/newsroom.xml
http://fs.jtbc.joins.com/RSS/morningand.xml
http://fs.jtbc.joins.com/RSS/newssite.xml
http://fs.jtbc.joins.com/RSS/politicaldesk.xml

 

 

package com.mcaronics.rss;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RssReader {

    private static RssReader instance = null;
    public static String ENCODING = "UTF-8";
    static String NL = System.getProperty("line.separator");
  
    private RssReader() {
    }
  
    public static RssReader getInstance() {
        if(instance == null) {
            instance = new RssReader();  
        }
        return instance;
    }
  
    public void writeNews() {
        try {

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//           URL u = new URL("http://mutasyon.net/cs/blogs/hakkiocal/rss.aspx");
//            URL u = new URL("http://rss.joins.com/joins_news_list.xml");
//            URL u = new URL("http://feeds.feedburner.com/afriken");
 //           URL u = new URL("http://blog.rss.naver.com/jeffyang.xml");
            URL u = new URL("http://fs.jtbc.joins.com/RSS/newsflash.xml");
           
            
            
            Document doc = builder.parse(u.openStream());
          
            NodeList nodes = doc.getElementsByTagName("item");
            NodeList nodeChannel = doc.getElementsByTagName("channel");
          
            for(int i=0;i<nodes.getLength();i++) {
              
                Element element = (Element)nodes.item(i);
              
                System.out.println("Title: " + getElementValue(element,"title"));
                System.out.println("Category: " + getElementValue(element,"category"));
                System.out.println("Link: " + getElementValue(element,"link"));
                System.out.println("Publish Date: " + getElementValue(element,"pubDate"));
                System.out.println("author: " + getElementValue(element,"author"));
                System.out.println("comment: " + getElementValue(element,"comments"));
                System.out.println("guid: " + getElementValue(element,"guid"));
                System.out.println("language: " + getElementValue(element,"language"));
                System.out.println("description: " + getElementValue(element,"description"));
                System.out.println();
                
                
            }//for          
        }//try
        catch(Exception ex) {
        ex.printStackTrace();  
        }
      
    }
  
    public void writeNews(String url) {
        try {
          
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            URL u = new URL(url);
          
            Document doc = builder.parse(u.openStream());
          
            NodeList nodes = doc.getElementsByTagName("item");
          
            for(int i=0;i<nodes.getLength();i++) {
              
                Element element = (Element)nodes.item(i);
              
                System.out.println("Title: " + getElementValue(element,"title"));
                System.out.println("Category: " + getElementValue(element,"category"));
                System.out.println("Link: " + getElementValue(element,"link"));
                System.out.println("Publish Date: " + getElementValue(element,"pubDate"));
                System.out.println("author: " + getElementValue(element,"author"));
                System.out.println("comment: " + getElementValue(element,"comments"));
                System.out.println("guid: " + getElementValue(element,"guid"));
                System.out.println("language: " + getElementValue(element,"language"));
                System.out.println("description: " + getElementValue(element,"description"));
                System.out.println();
            }//for          
        }//try
        catch(Exception ex) {
            ex.printStackTrace();  
        }
      
    }
      
      
    private String getCharacterDataFromElement(Element e) {
        try {
            Node child = e.getFirstChild();
            if(child instanceof CharacterData) {
                CharacterData cd = (CharacterData) child;
                return cd.getData();
            }
        }
        catch(Exception ex) {
          
        }
        return "";          
    } //private String getCharacterDataFromElement
  
    protected float getFloat(String value) {
        if(value != null && !value.equals("")) {
            return Float.parseFloat(value);  
        }
        return 0;
    }
  
    protected String getElementValue(Element parent,String label) {
        return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));  
    }
  
    /**
     * 파일 읽기
     * @param ctx
     * @param fileName
     * @return
     * @throws FileNotFoundException
     */
    public static StringBuffer readFile(String fileName, String fEncoding) throws FileNotFoundException {
        StringBuffer result = new StringBuffer();
//        Scanner scanner = new Scanner(new File(fileName), fEncoding);
        try {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            try {
                String line = null;
              
                while ((line=in.readLine()) != null) {
                    result.append(line+NL);
                }
            } finally {
                in.close();
            }
//            while (scanner.hasNextLine()) {
//                result.append(scanner.nextLine()+NL);
//            }
        }catch(IOException ex ) {
            ex.printStackTrace();
        }
      
        return result;
    }
  
    public static StringBuffer readFileScanner(String fileName, String fEncoding) throws FileNotFoundException {
        StringBuffer result = new StringBuffer();
        Scanner scanner = new Scanner(new File(fileName), fEncoding);
        try {
            while (scanner.hasNextLine()) {
                result.append(scanner.nextLine()+NL);
            }
        } finally {
                scanner.close();
        }
      
        return result;
    }
  
  /*  public static void main(String[] args) {
        try {
//            StringBuffer strUrl = readFile(args[0],  ENCODING);
        	
       	String rssTxt ="./data/rss.txt";
            StringBuffer strUrl = readFileScanner(rssTxt,  ENCODING);
            if(strUrl != null ){
                String[] urls = strUrl.toString().split(NL);
                for(int i=0; i < urls.length;i++) {
                    RssReader reader = RssReader.getInstance();
                    reader.writeNews(urls[i]);  
                }
            }

     //       RssReader reader = RssReader.getInstance();
       //     reader.writeNews();  
                      
        } catch (Exception e) {
            // TODO Auto-generated catch block
//            e.printStackTrace();
            System.out.println("지정된 파일을 찾을 수 없습니다. 파일을 확인하시기 바랍니다.");
        }

    }*/
}

 

 

about author

PHRASE

Level 60  머나먼나라

외상이면 소도 잡아먹는다 , 뒷일은 생각지 않고 당장 좋은 일이면 무턱대로 하고 본다는 말.

댓글 ( 0)

댓글 남기기

작성
  •    
  •