HD

네이버 Blog RSS data 사용 본문

javascript/네이버 블로그 RSS

네이버 Blog RSS data 사용

hunecenter 2019. 7. 29. 10:54
반응형

 

 
<%@page import="java.net.HttpURLConnection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.net.URL, org.jdom.Document, org.jdom.Element, org.jdom.input.SAXBuilder, org.xml.sax.InputSource, java.util.List, java.text.SimpleDateFormat, java.util.Date, java.util.Locale, java.text.ParseException  "%>
<%@ page import="java.net.URLConnection, java.io.InputStream, java.io.InputStreamReader, java.io.BufferedReader, java.io.File" %>

<%
List blogList = null;

SimpleDateFormat oldFormat = new SimpleDateFormat("EEE, dd MMM yy", Locale.ENGLISH);
SimpleDateFormat oldFormat2 = new SimpleDateFormat("dd MMM yy", Locale.ENGLISH);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = null;
URL url = null;


String bLink = "";
String bTitle = "";
String bDescription = "";
String bPubDate = "";
int bSize = 6;

try { // 블로그
	org.jdom.input.SAXBuilder parser = new org.jdom.input.SAXBuilder();
	parser.setValidation(false);
	parser.setIgnoringElementContentWhitespace(true);

 	url = new URL("해당 네이버 블로그 RSS URL");
	org.xml.sax.InputSource is = new org.xml.sax.InputSource(url.openStream());
	org.jdom.Document doc = parser.build(is);
	org.jdom.Element root = doc.getRootElement();
	org.jdom.Element channel = root.getChild("channel");
	blogList = channel.getChildren("item");

}catch(Exception e){
	e.printStackTrace();
}

%>

<!--naver-->
<li>
		<ul>
			<%
			if(blogList != null) {
				if(blogList.size() > bSize) {
					bSize = 8;
				} else {
					bSize = blogList.size();
				}
				for(int i=0; i < bSize ; i++) {
					org.jdom.Element el = (org.jdom.Element) blogList.get(i);
					bLink = el.getChildText("link");
					bTitle = el.getChildText("title");
					bDescription = el.getChildText("description");
					bPubDate = el.getChildText("pubDate");
					try {
						parsedDate = oldFormat.parse(bPubDate.substring(0,bPubDate.indexOf(":")-3));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					bPubDate = newFormat.format(parsedDate);
			%>
			<li>
				<a href="<%=bLink%>" title="새창 열기" target="_blank">
					<div>
						<p class="sns_content"><%=bDescription%></p>
						<p class="sns_date"><%=bPubDate%></p>
					</div>
					<!--이미지가 따로 없어서 default 이미지-->
					<img src="디폴트 이미지 경로" alt="이미지 alt">
				</a>
			</li>
			<%
				}
			}
			%>
		</ul>
</li>

 

네이버 RSS URL로 가져온 xml 형식의 데이터를 가져와서 ul, li 태그에 마춰 데이터를 뿌려주는 작업

반응형
Comments