详细看代码,部分涉及到隐私的就给删了,但是不影响功能,里面的日期等格式化,不知道的可以看我前面的博客
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map;
import org.apache.log4j.Logger;
import com.sun.syndication.feed.module.SipoModule; import com.sun.syndication.feed.module.SipoModuleImpl; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput;
public class RssUtil { private static Logger logger = Logger.getLogger(RssUtil.class);
public static String createXml(Map map) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Channel channel = new Channel("rss_2.0"); if (map.get("title") != null && !"".equals(map.get("title"))) channel.setTitle(map.get("title").toString()); if (map.get("") != null && !"".equals(map.get(""))) channel.setPubDate(format.parse((String) map.get(""))); if (map.get("") != null && !"".equals(map.get(""))) channel.setDescription(map.get("").toString()); if (map.get("link") != null && !"".equals(map.get("link"))) channel.setLink(map.get("link").toString());
channel.setEncoding("UTF-8"); channel.setLanguage("ZH_CN"); if (map.get("") != null && !"".equals(map.get(""))) channel.setCopyright(""); if (map.get("") != null && !"".equals(map.get(""))) channel.setPubDate(format.parse((String) map.get(""))); if (map.get("items") != null) { List arrays = (ArrayList) map.get("items"); List<Item> items = new ArrayList<Item>(); for (int i = 0; i < arrays.size(); i++) { Map itemMap = (Map) arrays.get(i); Item item = new Item(); Description description = new Description(); description.setValue(itemMap.get("description").toString()); if (itemMap.get("title") != null && !"".equals(itemMap.get("title"))) item.setTitle(itemMap.get("title").toString()); if (itemMap.get("url") != null && !"".equals(itemMap.get("url"))) item.setLink(itemMap.get("url").toString()); if (itemMap.get("legalDate") != null && !"".equals(itemMap.get("legalDate"))) item.setPubDate(format.parse(itemMap.get("legalDate").toString())); if (itemMap.get("description") != null && !"".equals(map.get("description"))) item.setDescription(description); List moduleList = item.getModules(); SipoModule sipomodule = new SipoModuleImpl();
moduleList.add(sipomodule); item.setModules(moduleList);
items.add(item); } if (!items.isEmpty()) channel.setItems(items); }
WireFeedOutput out = new WireFeedOutput(); try { String xml = out.outputString(channel); return xml; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return null; }
public static void main(String[] args) {
}
}
|
本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。
本文链接:https://royalscholar.cn/2017/03/28/Java生成RSS-XML文件/