** java]Http Åë½Å ÀÇ ¿¹Á¦ ÆÄÀÏ ÂüÁ¶
javascript¸¦ ÀÌ¿ëÇÑ ÃÊ°£´Ü Rss Reader ±¸ÇöÇϱâ : javascript ¸¦ ÀÌ¿ëÇÑ ÃÊ°£´Ü Rss Reader ±¸ÇöÇϱâ
ÂüÁ¶»çÀÌÆ® : http://blog.naver.com/sullim75?Redirect=Log&logNo=140028752352
춣ó : http://poolpiri.egloos.com/7021118
°£´ÜÇÑ Rss Reader ¸¸µé±â...
ºí·Î±×¸¦ ¸¸µé¾îº¸´Ù°¡ Rss¸¦ ÀÐ¾î¼ À¥»ó¿¡¼ º¸¿©ÁÖ´Â RssReader°¡ ÇÊ¿äÇÒ²¨ °°¾Æ¼...
À§ÀÇ ¼Ò½º´Â commons.digester 1.5/1.6 À» ±âÁØÀ¸·Î ÀÛ¼º µÇ¾î¼ 2.x¿¡¼´Â RSSDigester()¸¦ ãÀ» ¼ö ¾ø¾ú´Ù...(2010.07.06 ±âÁØ)°£´ÜÇÏ°Ô ¸¸µé¾îº¾´Ï´Ù.
ÀÚÄ«¸£Å¸ ¿ÀÇ ÄÄÆ÷³ÍÆ®Áß¿¡ ÇϳªÀÎ commons.digester¸¦ ÀÌ¿ëÇÑ°Ì´Ï´Ù...±×·¡¼ °£´ÜÇãÁ®...
ÀÏ´Ü ¾Æ·¡¿Í °°ÀÌ °£´ÜÇÑ ¸Þ¼µå¸¦ ¸¸µé¾îº¾´Ï´Ù.
/**
* RSSDigester¸¦ ÀÌ¿ëÇؼ ¸µÅ©ÀÇ ½ºÆ®¸²(xml)À» ÆĽÌÇؼ Channel°´Ã¼¸¦ ¸®ÅÏÇÑ´Ù.
*
* @param xmlLink
* @param response
* @return org.apache.commons.digester.rss.Channel
* @throws Exception
*/
private Channel getChannel(String xmlLink)
throws Exception {
Channel chan = null;
try {
RSSDigester digester = new RSSDigester();
// ¹æ¹ý 1. Rss Link¸¦ ÀÌ¿ëÇÑ ÆĽÌ...
//chan = (Channel) digester.parse(xmlLink);
// ¹æ¹ý 2. Rss Link¸¦ URL·Î ¿¬°áÇؼ ¾òÀº ½ºÆ®¸²À¸·Î ÆĽÌ...
//chan = (Channel) digester.parse(new URL(xmlLink).openStream());
// ¹æ¹ý 3. ±¹³» »çÀÌÆ® Character Encoding¹®Á¦ ¶§¹®¿¡...Ãß°¡...
chan = (Channel) digester.parse(getEncodedReader(xmlLink));
} catch (java.io.IOException ioe) {
chan = new Channel();
chan.setTitle("ERROR");
StringBuffer sb = new StringBuffer("<span class="error">");
sb.append("IOException parsing RSS XML LINK : " + xmlLink);
sb.append("</span>n<p>");
sb.append(ioe.toString());
chan.setDescription(sb.toString());
} catch (org.xml.sax.SAXException se) {
chan = new Channel();
chan.setTitle("ERROR");
StringBuffer sb = new StringBuffer("<span class="error">");
sb.append("SAXException parsing RSS XML LINK : " + xmlLink);
sb.append("</span>n<p>");
sb.append(se.toString());
chan.setDescription(sb.toString());
}
return chan;
}
* RSSDigester¸¦ ÀÌ¿ëÇؼ ¸µÅ©ÀÇ ½ºÆ®¸²(xml)À» ÆĽÌÇؼ Channel°´Ã¼¸¦ ¸®ÅÏÇÑ´Ù.
*
* @param xmlLink
* @param response
* @return org.apache.commons.digester.rss.Channel
* @throws Exception
*/
private Channel getChannel(String xmlLink)
throws Exception {
Channel chan = null;
try {
RSSDigester digester = new RSSDigester();
// ¹æ¹ý 1. Rss Link¸¦ ÀÌ¿ëÇÑ ÆĽÌ...
//chan = (Channel) digester.parse(xmlLink);
// ¹æ¹ý 2. Rss Link¸¦ URL·Î ¿¬°áÇؼ ¾òÀº ½ºÆ®¸²À¸·Î ÆĽÌ...
//chan = (Channel) digester.parse(new URL(xmlLink).openStream());
// ¹æ¹ý 3. ±¹³» »çÀÌÆ® Character Encoding¹®Á¦ ¶§¹®¿¡...Ãß°¡...
chan = (Channel) digester.parse(getEncodedReader(xmlLink));
} catch (java.io.IOException ioe) {
chan = new Channel();
chan.setTitle("ERROR");
StringBuffer sb = new StringBuffer("<span class="error">");
sb.append("IOException parsing RSS XML LINK : " + xmlLink);
sb.append("</span>n<p>");
sb.append(ioe.toString());
chan.setDescription(sb.toString());
} catch (org.xml.sax.SAXException se) {
chan = new Channel();
chan.setTitle("ERROR");
StringBuffer sb = new StringBuffer("<span class="error">");
sb.append("SAXException parsing RSS XML LINK : " + xmlLink);
sb.append("</span>n<p>");
sb.append(se.toString());
chan.setDescription(sb.toString());
}
return chan;
}
ÆĶó¹ÌÅÍ·Î ¿Í¾ßÇÏ´Â String xmlLinkÀº º» ¿¥ÆĽº ºí·Î±×¿¡ Àִ°Ű°Àº RSS¸µÅ© À¯¾Ë¿¤À» ³Ö¾îÁÖ½É µË´Ï´Ù. Á¦ ºí·Î±×¸¦ ¿¹¸¦ µé¸é..."http://blog.empas.com/poolpiri3/rss.xml" ÀÌ°Ô µé¾î¿À¸é µÇ´Â°Ì´Ï´Ù.
¿©±â ¿¹Á¦´Â ¿¥ÆĽº¿¡¼ Á¦°øÇÏ´Â rss.2.0 ¹öÀü¿¡¸¸ Àßµ¹¾Æ°©´Ï´ç...ÀÌ ¹öÀüÀº À¢¸¸ÇÑ ±¹³» RSS°¡ Áö¿øÇÏ´Â ¹öÀüÀÔ´Ï´Ù...
¼Ò½º Áß°£¿¡ ÀÖ´Â ÁÖ¼®À» º¸½Ã¸é ¸î°¡Áö ¹æ¹ýÀÌ ÀÖ½À´Ï´Ù¸¸...±¹³» »çÀÌÆ®ÀÇ °æ¿ì ÇÑ±Û ÀÎÄÚµù ¹®Á¦·Î ÆĽÌÀÌ Á¦´ë·Î ¾ÈµË´Ï´Ù. ¿©±â ¿¥ÆĽºµµ ¸¶Âù°¡Áö±¸¿©... ÀÌ·²ÁÙ ¾Ë¾ÒÀ¸¸é ±»ÀÌ digester¸¦ ¾²Áö ¾Ê°í ¹æ¹ýÀ» »ý°¢ÇßÀ¸¸é ´õ ½¬¿öÁú ¼ö µµ ÀÖ¾ú½À´Ï´Ù¸¸...Á¦°¡ ¹«ÁöÇÑÁö¶ó...ÇÏ¿© ¾Æ·¡¿Í °°Àº ¸Þ¼µå¸¦ ¹«½ÄÇÏ°Ô Ãß°¡Çß½À´Ï´Ù.
¿©±â ¿¹Á¦´Â ¿¥ÆĽº¿¡¼ Á¦°øÇÏ´Â rss.2.0 ¹öÀü¿¡¸¸ Àßµ¹¾Æ°©´Ï´ç...ÀÌ ¹öÀüÀº À¢¸¸ÇÑ ±¹³» RSS°¡ Áö¿øÇÏ´Â ¹öÀüÀÔ´Ï´Ù...
¼Ò½º Áß°£¿¡ ÀÖ´Â ÁÖ¼®À» º¸½Ã¸é ¸î°¡Áö ¹æ¹ýÀÌ ÀÖ½À´Ï´Ù¸¸...±¹³» »çÀÌÆ®ÀÇ °æ¿ì ÇÑ±Û ÀÎÄÚµù ¹®Á¦·Î ÆĽÌÀÌ Á¦´ë·Î ¾ÈµË´Ï´Ù. ¿©±â ¿¥ÆĽºµµ ¸¶Âù°¡Áö±¸¿©... ÀÌ·²ÁÙ ¾Ë¾ÒÀ¸¸é ±»ÀÌ digester¸¦ ¾²Áö ¾Ê°í ¹æ¹ýÀ» »ý°¢ÇßÀ¸¸é ´õ ½¬¿öÁú ¼ö µµ ÀÖ¾ú½À´Ï´Ù¸¸...Á¦°¡ ¹«ÁöÇÑÁö¶ó...ÇÏ¿© ¾Æ·¡¿Í °°Àº ¸Þ¼µå¸¦ ¹«½ÄÇÏ°Ô Ãß°¡Çß½À´Ï´Ù.
/**
* xmlÆÄÀÏÀÇ encodingŸÀÔÀ» ÀÐ¾î¼ ±× ŸÀÔ´ë·Î ÄÁ¹öÀüÈÄ InputStreamReader¸¦ ¸®ÅÏÇÑ´Ù.
*
* @param xmlLink
* @return InputStreamReader
*/
private InputStreamReader getEncodedReader(String xmlLink) {
URL url = null;
InputStreamReader reader = null;
BufferedReader br = null;
try {
url = new URL(xmlLink);
reader = new InputStreamReader(url.openStream());
byte b[] = new byte[1024];
br = new BufferedReader(reader);
String line = br.readLine();
while (line != null) {
if (!(line.trim().equals(""))) {
int index = line.toLowerCase().indexOf("encoding");
String prefix = """;
String encoding = "";
if (index != -1) {
int start = line.indexOf(prefix, index) + 1;
if (start == -1) {
prefix = "'";
start = line.indexOf(prefix, index) + 1;
if (start == -1) {
start = line.indexOf("=", index) + 1;
prefix = "?";
}
}
encoding = line.substring(start, line.indexOf(prefix, start));
if (encoding != null && !("".equals(encoding))) {
reader = new InputStreamReader( url.openStream(), encoding.trim());
} else {
reader = new InputStreamReader(url.openStream());
}
} else {
reader = new InputStreamReader(url.openStream());
}
break;
}
line = br.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return reader;
}
* xmlÆÄÀÏÀÇ encodingŸÀÔÀ» ÀÐ¾î¼ ±× ŸÀÔ´ë·Î ÄÁ¹öÀüÈÄ InputStreamReader¸¦ ¸®ÅÏÇÑ´Ù.
*
* @param xmlLink
* @return InputStreamReader
*/
private InputStreamReader getEncodedReader(String xmlLink) {
URL url = null;
InputStreamReader reader = null;
BufferedReader br = null;
try {
url = new URL(xmlLink);
reader = new InputStreamReader(url.openStream());
byte b[] = new byte[1024];
br = new BufferedReader(reader);
String line = br.readLine();
while (line != null) {
if (!(line.trim().equals(""))) {
int index = line.toLowerCase().indexOf("encoding");
String prefix = """;
String encoding = "";
if (index != -1) {
int start = line.indexOf(prefix, index) + 1;
if (start == -1) {
prefix = "'";
start = line.indexOf(prefix, index) + 1;
if (start == -1) {
start = line.indexOf("=", index) + 1;
prefix = "?";
}
}
encoding = line.substring(start, line.indexOf(prefix, start));
if (encoding != null && !("".equals(encoding))) {
reader = new InputStreamReader( url.openStream(), encoding.trim());
} else {
reader = new InputStreamReader(url.openStream());
}
} else {
reader = new InputStreamReader(url.openStream());
}
break;
}
line = br.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return reader;
}
ÇÏ¿©Æ° ÀÌ·¸°Ô ¾ò¾îÁø channel¸¦ °´Ã¼¸¦ ÅëÇؼ ¾Æ·¡¿Í °°ÀÌ jsp¿¡¼ »Ñ·ÁÁÖ½É µË´Ï´Ù.
<% int maxItems = 10;
if (channel != null) {
if ("ERROR".equals(channel.getTitle())) {%>
<tr bgcolor="#FFCCFF">
<td width="100" bgcolor="#CCCCFF">ºí·Î±× À̸§</td>
<td bgcolor="#CCFFCC"><%=channel.getDescription()%></td>
</tr>
<%return;
}%><% String divId = Utility.replace(channel.getTitle(), " ", "_");
divId = Utility.replace(channel.getTitle(), "'", "_");%>
<!--<%=channel.getLastBuildDate()%><%=channel.getCopyright()%><%=channel.getDocs()%><%=channel.getLanguage()%>-->
<tr bgcolor="#FFCCFF">
<td bgcolor="#CCFFCC" width="500" colspan="2" valign="bottom"><a href="<%=channel.getLink()%>" target="_blank"><h3><%=channel.getTitle()%></h3></a><br>
<%=channel.getDescription()%></td>
</tr>
<% Image image = channel.getImage();
if (image != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2" height="50"><img src="<%=image.getURL()%>" alt="<%=image.getTitle()%>" > <!--width="<%=image.getWidth()%>" height="<%=image.getHeight()%>"-->
</td>
</tr>
<% }
Item[] items = channel.getItems();
for (int j = 0; j < items.length; j++) {if (items[j].getTitle() != null && items[j].getLink() != null) {%>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="<%=items[j].getLink()%>" target="_blank" class="RssLink"><%=items[j].getTitle()%></a></td>
</tr>
<% } else if (items[j].getTitle() != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2"><div class="RssTitle"><%=items[j].getTitle()%></div></td>
</tr>
<% }
if (items[j].getDescription() != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2"><div class="RssDesc"><%=items[j].getDescription()%>...</div></td>
</tr>
<% }
if (maxItems != -1 && j >= maxItems)
break;
}
} else {%>
<tr bgcolor="#FFFFFF">
<td colspan="2">ºí·Î±× ¸®½ºÆ®°¡ Çϳªµµ ¾ø½À´Ï´Ù. -_-</td>
</tr>
<% }%>
if (channel != null) {
if ("ERROR".equals(channel.getTitle())) {%>
<tr bgcolor="#FFCCFF">
<td width="100" bgcolor="#CCCCFF">ºí·Î±× À̸§</td>
<td bgcolor="#CCFFCC"><%=channel.getDescription()%></td>
</tr>
<%return;
}%><% String divId = Utility.replace(channel.getTitle(), " ", "_");
divId = Utility.replace(channel.getTitle(), "'", "_");%>
<!--<%=channel.getLastBuildDate()%><%=channel.getCopyright()%><%=channel.getDocs()%><%=channel.getLanguage()%>-->
<tr bgcolor="#FFCCFF">
<td bgcolor="#CCFFCC" width="500" colspan="2" valign="bottom"><a href="<%=channel.getLink()%>" target="_blank"><h3><%=channel.getTitle()%></h3></a><br>
<%=channel.getDescription()%></td>
</tr>
<% Image image = channel.getImage();
if (image != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2" height="50"><img src="<%=image.getURL()%>" alt="<%=image.getTitle()%>" > <!--width="<%=image.getWidth()%>" height="<%=image.getHeight()%>"-->
</td>
</tr>
<% }
Item[] items = channel.getItems();
for (int j = 0; j < items.length; j++) {if (items[j].getTitle() != null && items[j].getLink() != null) {%>
<tr bgcolor="#FFFFFF">
<td colspan="2"><a href="<%=items[j].getLink()%>" target="_blank" class="RssLink"><%=items[j].getTitle()%></a></td>
</tr>
<% } else if (items[j].getTitle() != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2"><div class="RssTitle"><%=items[j].getTitle()%></div></td>
</tr>
<% }
if (items[j].getDescription() != null) {%><tr bgcolor="#FFFFFF">
<td colspan="2"><div class="RssDesc"><%=items[j].getDescription()%>...</div></td>
</tr>
<% }
if (maxItems != -1 && j >= maxItems)
break;
}
} else {%>
<tr bgcolor="#FFFFFF">
<td colspan="2">ºí·Î±× ¸®½ºÆ®°¡ Çϳªµµ ¾ø½À´Ï´Ù. -_-</td>
</tr>
<% }%>
ÈìÈì...Á¦°¡ ºÁµµ ¾î¼³ÇÁ³×¿©...
ÀÌ·¸°Ô Çϸé ÀÚ±â ȨÆäÀÌÁö³ª ƯÁ¤»çÀÌÆ®¿¡ ±âÁ¸¿¡ IFRAME°°Àº ¹æ¹ýÀ¸·Î ³Ö¾îÁá´ø ´Ù¸¥ »çÀÌÆ®ÀÇ Æ¯Á¤ °Ô½Ã¹° ¸®½ºÆ®¸¦ RSS¿Í RSS Reader¸¦ ÅëÇؼ Ãâ·ÂÇØÁÙ ¼ö ÀÖ½À´Ï´Ù.
¹°·Ð Ç×»ó ½Å±Ô ¸®½ºÆ®°ÚÁÒ ¶ÇÇÑ ÇØ´ç »çÀÌÆ®¿¡¼ RSS¸¦ Áö¿øÇؾ߸¸ °¡´ÉÇѰű¸¿©...¿ä»÷ »ó´ç¼ö »çÀÌÆ®µéÀÌ Áö¿øÀ» Çϱ¸¿©...
±×·³...
±×·¡¼ ¾Æ·¡ÀÇ ¼Ò½º·Î rss reader¸¦ ±¸ÇöÇÏ°Ô µÇ¾ú´Ù...
RssReader.java
=================================
package 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;
private 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");
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);
StringBuffer strUrl = readFileScanner(args[0], 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 (FileNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("ÁöÁ¤µÈ ÆÄÀÏÀ» ãÀ» ¼ö ¾ø½À´Ï´Ù. ÆÄÀÏÀ» È®ÀÎÇϽñ⠹ٶø´Ï´Ù.");
}
}
}