`
SunShineBoy
  • 浏览: 44883 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JDOM生成百度新闻XML (一)

阅读更多
百度新闻规则样式页:http://news.baidu.com/newsop.html

首先要导入jdom 相关的包;接下来就是在后台写相关的信息:我用是struts,就以struts后台action为例生成相应的xml;

public String perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/* 设置返回文件的类型,可以是xml,html等等 */
  response.setContentType("text/xml;charset=gb2312");
/**
*  不要让浏览器开辟缓存
*  Cache-Control  no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存
*  Expires属性是将页面缓存在客户端,在有效时间内用户第二次打开该页面,直接调用缓存的页面而不用再请求服务器.执行速度就快了.
*  Pragma no-cache本地就不会缓存,即每次请求的都是最新版本
*/
  response.setHeader("Cache-Control","no-cache");

/* 这个是从后台Bo读取的数据放在pagination */
Pagination pagination = newsBo.queryNews(1, 10);

/* Jdom开始 包括各个根节点 */
Element root,webSite,webMaster,updatePeri,item,title,link,description,text,image,headlineImg,
keywords,category,author,source,pubDate;       

root = new Element("document"); //生成根元素:

webSite = new Element("webSite"); //生成二级元素:                          

webMaster = new Element("webMaster"); //生成二级元素

updatePeri = new Element("updatePeri"); //生成二级元素

Document doc = new Document(root); //将根元素植入文档doc中
/* 设置二级元素webSite的值 */
webSite.setText("info.uns56.com");
/* 将二级元素webSite放到root目录下 */
root.addContent(webSite);

/* 设置二级元素webMaster的值 */
webMaster.setText("www.sunwu.com");
/* 将二级元素webMastere放到root目录下 */
root.addContent(webMaster);

/* 设置二级元素updatePeri的值 */
updatePeri.setText("60");
/* 将二级元素updatePeri放到root目录下 */
root.addContent(updatePeri);
/* 循环从数据库取出数据 */

for(int i = 0; i < pagination.getResult().size(); i++){
/* 把从数据库取出的数据转换成相应的java bean */
Note  notes = (Note) pagination.getResult().get(i);

    item = new Element("item");   //生成三级元素 以下同
    title = new Element("title");
    link =new Element("link");
    description =new Element("description");
    text =new Element("text");
    image =new Element("image");
    headlineImg =new Element("headlineImg");
    keywords =new Element("keywords");
    category =new Element("category");
    author =new Element("author");
    source =new Element("source");
    pubDate =new Element("pubDate");
/* 设置三级元素的值 */
title.setText(notes.getTitle());
link.setText(notes.getUrl());
description.setText(notes.getDigest());
/* 用apache下的一个类判断是否为空*/
if(StringUtils.isNotBlank(notes.getContent())){
/* 替换里面相应的html内容 */
String content = notes.getContent().replaceAll("<[^>]*>", "").replace("&nbsp;", "").replace("&ldquo;", "\"").replace("&rdquo;", "\"").replace("&lsquo;", "\'").replace("&rsquo;", "\'").replace("&lt;", "<").replace("&gt;", ">");
text.setText("<![CDATA["+content+"]]>");
}
image.setText("");
headlineImg.setText("");

/** 迭代循环出关键字
*  如果没有使用Iterator,遍历一个数组的方法是使用索引
*  eg:for(int i=0; i<array.size(); i++) { ... get(i) ... }
*  而访问一个链表(LinkedList)又必须使用while循环:
*  eg:while((e=e.next())!=null) { ... e.data() ... }
*/
Iterator words = notes.getWords().iterator();
StringBuffer sb = new StringBuffer();
if(words.hasNext()){
sb.append(words.next().toString());
while(words.hasNext()){
sb.append(' ').append(words.next());
}
}
keywords.setText(sb.toString());
/* 用map遍历session里面的东西 */
Map map = (Map)servlet.getServletContext().getAttribute("options");

/* 得到map里notes_type的值 转换成list */
List list =  (List) map.get("notes_type");
OptionItem opt=null;
for(Iterator it=list.iterator();it.hasNext();){
opt=(OptionItem)it.next();
if(opt.getItemKey().equals(notes.getType()))
category.setText(opt.getItemValue());
}


author.setText("呵呵网站");
source.setText("呵呵网站");
/* 把时间转换成 2008-10-10 15:02:19 格式 */
pubDate.setText((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(notes.getPublishDate()));
/* 读取系统配置文件 */
link.setText("http://"+SystemConfigHandler.getString("HEHE_NAME")+"/"+notes.getNoteId()+".htm");

/
item.addContent(title);
item.addContent(link);
item.addContent(description);
item.addContent(text);
item.addContent(image);
item.addContent(headlineImg);
item.addContent(keywords);
item.addContent(category);
item.addContent(author);
item.addContent(source);
item.addContent(pubDate);
root.addContent(item);

}

Format format = Format.getCompactFormat();

format.setEncoding("gb2312"); //设置xml文件的字符为gb2312

format.setIndent("    "); //设置xml文件的缩进为4个空格

XMLOutputter XMLOut = new XMLOutputter(format); //元素后换行一层元素缩四格
System.out.println(XMLOut.outputString(doc)); //在后台打印出xml文件

XMLOut.output(doc, response.getWriter()); //前台输出xml文件
XMLOut.output(doc, new FileOutputStream("D:\\newsXml.xml")); //在D盘下生成xml

}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics