[java] 动态创建RSS

package com.baosight.iplat4j.ec.rs.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.baosight.iplat4j.common.ec.domain.Tecsm01;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ec.rs.domain.ECRS01;
import com.baosight.iplat4j.ec.service.ServiceEC0001;
import com.baosight.iplat4j.ec.tm.utils.TemplateUtils;
import com.baosight.iplat4j.ec.util.ECUtil;
import com.baosight.iplat4j.logging.Logger;
import com.baosight.iplat4j.logging.LoggerFactory;
   
public class ServiceECRS02 extends ServiceECRS01{
 private static final Logger logger = LoggerFactory.getLogger(ServiceECRS02.class);

 /**
  * 动态生成Rss文件
  * @param info  List<文章列表>  name<为rss命名>
  * @return
  * @throws IOException
  * @throws JDOMException
  */
 public EiInfo BuildXMLDoc(EiInfo info) throws IOException, JDOMException {
     EiInfo outInfo = new EiInfo();
     ServiceEC0001 service = new ServiceEC0001();
  try {
   List list = (List) info.get("articleList");
   String name = info.getString("name");
   
   if (list != null && list.size() > 0) {
    ECUtil util = new ECUtil();
    ECRS01 rs = (ECRS01) list.get(0);

    //获取站点对象
    Tecsm01 tecsm01 = (Tecsm01) util.getSiteByArtId(rs.getLink());
    //_rss创建站点别名子文件夹
    String url = TemplateUtils.getWebContentPath() + "_rss/"+ tecsm01.getSiteAlias() + "/";
    File file = new File(url);
    if (!file.exists()) {
     file.mkdir();
    }
    //访问站点URL
    String url1 = service.cutString(TemplateUtils.getSiteLink(tecsm01.getSiteId()))+"/index.html";
    
    //创建根节点    
    Element root = new Element("root");
    //创建子节点
    Element channel = new Element("channel");
    //子节点添加属性,创建rss头部所需元素。
    channel.addContent(new Element("title").setText(tecsm01.getSiteName()));
    channel.addContent(new Element("link").setText(url1));
    channel.addContent(new Element("description").setText(""));
    //channel节点添加到root节点中;
    root.addContent(channel);    
    //root节点添加到文档中;
    Document Doc = new Document(root);

    // 此处 for 循环可替换成 遍历 数据库表的结果集操作;   
    for (int j = 0; j < list.size(); j++) {
     rs = (ECRS01) list.get(j);

     String path = service.cutString(TemplateUtils.getArticleMonthLinkById(rs.getLink()));

     //日期格式化
     String date = rs.getPubDate();
     String date1 = date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8)+" "+
           date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12);
     
     // 创建节点 item;   
     Element elements = new Element("item");
     
     // 给 item 节点添加子节点并赋值;       
     elements.addContent(new Element("title").setText(rs.getTitle()));
     elements.addContent(new Element("link").setText(path));
     elements.addContent(new Element("pubDate").setText(date1));
     elements.addContent(new Element("author").setText(rs.getAuthor()));
                   
     ServiceEC0001 serviceEC0001 = new ServiceEC0001();
     String textStr = serviceEC0001.Html2Text(rs.getDescription());
     if (textStr.length() > 100) {
      textStr = textStr.substring(0, 100) + "..";
     }
     elements.addContent(new Element("description").setText(textStr));

     //将item添加到channel节点中
     channel.addContent(elements);
    }
    XMLOutputter XMLOut = new XMLOutputter();
    //1.首行缩进对输出格式进行美化 2.处理Xml中文编码问题。
    Format format = Format.getPrettyFormat();
    format.setEncoding("utf-8");
    XMLOut.setFormat(format);

    // 输出 rss 文件;
    XMLOut.output(Doc, new FileOutputStream(file + "/"+ name + ".xml"));
    outInfo.setStatus(1);
    outInfo.setMsg("创建Rss文件成功!");
   }
  } catch (Exception e) {
   outInfo.setStatus(-1);
   outInfo.setMsg("------------------创建Rss文件失败!------------------");
   e.printStackTrace();
  }  
  return outInfo;
    }    
}

原文地址:https://www.cnblogs.com/kentyouyou/p/2957922.html