创建rss1(不用rome组件)

private void bildXML(String filePath, String thechannel, List list, String fileName)
{
Properties properties = UtilProperties.getProperties("url.properties");
//获得标题的链接地址
String urlpath = properties.getProperty("urlpath");
//获得终端的访问地址
String huaweiUrl = properties.getProperty("huaweiUrl");

String tempFilePath = filePath + File.separator + fileName + ".xml";
if (!new File(filePath).isDirectory())
{
new File(filePath).mkdirs();
}

Document doc = org.dom4j.DocumentHelper.createDocument();
//创建节点操作对象根节点rss
Element rootElement = doc.addElement("rss");
rootElement.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
rootElement.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElement.addAttribute("version", "2.0");

//创建频道
Element channel = rootElement.addElement("channel");
Element title = channel.addElement("title");
title.setText(fileName + "- HUAWEI Communication");
Element link = channel.addElement("link");
link.setText(huaweiUrl);
Element description = channel.addElement("description");
description.setText("Hwawei Website Rss Feed");
Element language = channel.addElement("language");
language.setText("en");
Element copyright = channel.addElement("copyright");
copyright.setText("Huawei Communication Technologies Co., 2009 All rights reserved");
Element pubDate = channel.addElement("pubDate");
pubDate.setText((new Date()).toString());
//循环取出频道信息,分别设置item
for (int i = 0; i < list.size(); i++)
{

RssInfo rssInfo = (RssInfo) list.get(i);
Element item = channel.addElement("item");
//设置标题
Element itemTitle = item.addElement("title");
if (!"".equals(rssInfo.getChannelItem().getTitle()) && null != rssInfo.getChannelItem().getTitle())
{
itemTitle.setText(rssInfo.getChannelItem().getTitle());
}
{
Element itemPubDate = item.addElement("pubDate");

itemPubDate.setText(rssInfo.getChannelItem().getPubDate());
}
}

try
{
OutputFormat fmt = new OutputFormat();
//创建输出格式对象
fmt.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(fmt);
//以输出格式为参数,创建XML文件输出对象
OutputStream out = new FileOutputStream(tempFilePath);
//创建输出流..
writer.setOutputStream(out);
//设置输出流
writer.write(doc);
//输出doc对象,即形成XML文件
}
catch (Exception e)
{

e.printStackTrace();
}

}

你可能感兴趣的:(xml)