스프링

'xml' 의미를 부여한 태그 답게 그냥 적정한 태그에 적정한 값을 넣어 주면 끝처럼 보였습니다.3

 

<?xml version="1.0" encoding="UTF-8"?>
   <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
      <channel>
         <title>사이트 제목</title>
         <link>사이트 주소</link>
         <description>RSS 명</description>
         <copyright>관리자 이메일</copyright>
         <pubDate>날짜</pubDate>
         <webMaster>웹마스터 이메일</webMaster>
         <!-- 언어 -->
         <language>ko</language>  
      <item>
         <title>글 제목</title>
         <link>http://글 링크</link>
         <description>글 내용</description>
         <category>카테고리</category>
         <guid>유일한 글식별번호</guid>
         <pubDate>날짜</pubDate>
      </item>
      <item>
          ...
      </item>
           ...
    </channel>
  </rss> 

 

대충 위에 내용 대로 넣어 xml파일을 뿌려주면 RSS 리더기가 필요한 태그를 쏙쏙 빼가서 유용하게 

보여주는 구조입니다.

http://www.rssboard.org/rss-specification

위 링크로 가면 RSS 태그 규격을 볼 수 있습니다. 물론 영어 ㅎㅎ

간단 할 줄 알았는데 막상 구현 할려니 정해야 할게 몇게 있더군요,

우선 실제 xml파일을 생성해 RSS를 구현 할 것인지, 

아니면 JavaScript나 다른 방법으로 가상의 xml파일을 만들어 보여주기만 할 것인지 

후자 쪽은 잘 모르겠어서 전자 xml파일을 실제로 생성하고 사용자가 해당 파일로 접근해 RSS를 

배포하는 식으로 만들어 봤습니다.

두 번째로 정해야 할 것은 게시판 글이나 기타 RSS로 배포해야 하는 컨텐츠가 등록 될 때마다 xml 파일을

업데이트 할 것인지 아니면 사용자가 xml을 호출 할때 마다 xml파일을 업데이트 할 것 인지입니다.

개인적으로 등록될 때마다 업데이트 하는게 맞다고 생각하지만 일단 호출 될 때 마다 xml파일을 업데이트

하게 만들었습니다. 특정 url 호출이 있으면 jdom을 이용해 xml 파일을 생성하게 했습니다.

 

 jdom.jar

위의 파일은 jdom 라이블러리 입니다.

 

스프링 Pom.xml

<!-- 안드로이드 연동 xml - json -->
<!-- https://mvnrepository.com/artifact/org.jdom/jdom -->
 <!-- xml 생성 -->
<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>2.0.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<!-- <dependency>  상단에 추가  json 객체 생성
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
</dependency>
 -->
<!-- jackson-databind는 상단에 추가 하였음 -->

<!-- jackson-databind 는 @ResponseBody json 타입으로 리턴하기 위한것 -->


<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.4</version>
</dependency>

<!-- 구글 JSON 라이브러리 -->
<dependency>
	<groupId>com.googlecode.json-simple</groupId>
	<artifactId>json-simple</artifactId>
	<version>1.1</version>
</dependency>

 

public void createRss(String rssFile)  {

   Element root = new Element("rss");
   Attribute version = new Attribute("version", "2.0"); 
          //  Attribute xmlns = new Attribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
   root.setAttribute(version);
          //  root.setAttribute(xmlns);
  
   Element channel = new Element("channel");
   Element title = new Element("title");
   title.setText("INTSYSTEM 소스관리 시스템 RSS");
   Element link = new Element("link");
   link.setText(http://naver.com);
   Element description = new Element("description");
   description.setText("소스 목록");
   Element copyright = new Element("copyright");
   copyright.setText("kjw2043@main.co.kr");
   Element webMaster = new Element("webMaster");
   webMaster.setText("kjw2043@main.co.kr");
   Element language = new Element("language");
   language.setText("ko");
  
   Element item = new Element("item");
   Element ititle = new Element("title");
   ititle.setText("제목");
   Element ilink = new Element("link");
   ilink.setText("http://intsystem.co.kr");
   Element idescription = new Element("description");
   idescription.setText("내용");
   Element category = new Element("category");
   category.setText("카테고리");
   Element guid = new Element("guid");
   guid.setText("1");
   Element pubDate = new Element("pubDate");
   pubDate.setText("2010.06.11");
   item.addContent(ititle);
   item.addContent(ilink);
   item.addContent(idescription);
   item.addContent(category);
   item.addContent(guid);
   item.addContent(pubDate);
   
   channel.addContent(item);
   channel.addContent(title);
   channel.addContent(link);
   channel.addContent(description);
   channel.addContent(copyright);
   channel.addContent(webMaster);
   channel.addContent(language);  
   root.setContent(channel);
  
   Document doc = new Document();
   doc.setRootElement(root);  
   XMLOutputter outputter = new XMLOutputter();  
     Format f = outputter.getFormat();
     f.setEncoding("euc-kr");
     f.setIndent(" ");
     f.setLineSeparator("\r\n");
     f.setTextMode(Format.TextMode.TRIM);
     outputter.setFormat(f);
     FileWriter writer;

  try {
   writer = new FileWriter(rssFile);
   outputter.output(doc, writer);
   writer.close();  
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 } 

 

 

 

위 소스가 간단한 RSS 생성 소스입니다.

RSS xml을 호출 받으면 public void createRss(String rssFile) 메소드를 호출하고 여기서 rssFile은 

xml파일의 경로입니다. 

여기서 중요한 점이 두군데 있는데 

하나는 주석처리된 아래 부분입니다.

 

//  Attribute xmlns = new Attribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");

 

 

RSS 표준에는 rss루트 엘리먼트에 위의 속성이 들어가야 하지만 어째서인지 jdom에서는 위의 속성이 

오류가 납니다.  해결법을 아시는 분은 알려 주세요.

두 번째는

 

Format f = outputter.getFormat();

f.setEncoding("euc-kr");

 

위의 format의 인코딩을 바꿔주는 부분입니다.

처음에는 위 처럼 인코딩을 바꿔주는 것을 몰라 계속 오류가 낳습니다.

RSS 내용에 한글이 포함되어 있다면 인코딩을 euc-kr로 바꿔주세요.

//Document 타입을 스트링으로 변환
		//org.jdom2
		//xml 출력을 위한 객체
		XMLOutputter xout =new XMLOutputter();
		Format f=xout.getFormat();//xml 문서의 포맷 설정
		f.setEncoding("utf-8");	//인코딩 방식 설정
		f.setIndent("\t");//들여쓰기 문자
		f.setLineSeparator("\r\n"); //줄바꿈 문자
		f.setTextMode(Format.TextMode.TRIM); //공백제거
		xout.setFormat(f);//문서 포맷 적용
		//Document 를 스트링으로 변환
		result=xout.outputString(doc);

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

군자는 시기를 기다려서 좋은 기회를 포착하여 행동한다. 그 시기까지는 필요한 준비를 갖춤으로써 실패없는 성공으로 이끄는 길이 된다. -역경

댓글 ( 4)

댓글 남기기

작성