package com.jy.util;
import java.io.IOException; import java.util.Iterator; import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.tree.DefaultDocument;
/** * Title:NoteRssXml.java * Description: * Copyright:Copyright (c) 2009 * Company:佳誉科技有限公司 * * @author 吴瑜明 * @version Revision:1.0 Date:May 4, 2009 */ public class NoteRssXml { public static final Log LOG = LogFactory.getLog(NoteRssXml.class); private final static String RSS = "rss"; private final static String VERSION = "version"; private final static String VERSIONVALUE = "2.0"; private final static String CHANNEL = "channel"; private final static String TITLE = "title"; private final static String TITLEVALUE = "社区通知"; private final static String LINK = "link"; private final static String LINKVALUE = ""; private final static String DESCRIPTION = "description"; private final static String DESCRIPTIONVALUE = "社区通知";
private final static String ITEM = "item"; private final static String ITEMID = "id"; private final static String ITEMTITLE = "title"; private final static String ITEMTLINK = "link"; private final static String ITEMTDESCRIPTION = "description"; private final static String ITEMTDPUBDATE = "pubDate"; private final static String Encoding = "GBK"; private XmlDom4J xmlDom4J; public NoteRssXml() { xmlDom4J = new XmlDom4J(); } /** * 功能说明:创建Document * @author fisher * @param fileName 文件路径 * @return * */ public void createDocument(String fileName){ Document document = new DefaultDocument(); Element rssElement = document.addElement(RSS); rssElement.addAttribute(VERSION, VERSIONVALUE); Element channelElement = rssElement.addElement(CHANNEL); Element titleElement = channelElement.addElement(TITLE); titleElement.setText(TITLEVALUE); Element linkElement = channelElement.addElement(LINK); linkElement.setText(LINKVALUE); Element descriptionElement = channelElement.addElement(DESCRIPTION); descriptionElement.setText(DESCRIPTIONVALUE);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } } /** * 功能说明:增加元素 * @author fisher * @param fileName 文件路径 * @param idValue item节点的id值 * @param titleValue item节点的title值 * @param linkValue item节点的link值 * @param descriptionValue item节点的description值 * @param pubdateValue item节点的pubdate值 * @return * */ @SuppressWarnings("unchecked") public void addElement(String fileName, String id, String title, String link, String description, String pubdate) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; Element itemElement = channelElement.addElement(ITEM); itemElement.addAttribute(ITEMID, id); Element titleElement = itemElement.addElement(ITEMTITLE); titleElement.setText(title); Element linkElement = itemElement.addElement(ITEMTLINK); linkElement.setText(link); Element descriptionElement = itemElement.addElement(ITEMTDESCRIPTION); descriptionElement.setText(description); Element pubDateElement = itemElement.addElement(ITEMTDPUBDATE); pubDateElement.setText(pubdate);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } } /** * 功能说明:增加元素 * @author fisher * @param fileName 文件路径 * @param document 文档 * @param idValue item节点的id值 * @param titleValue item节点的title值 * @param linkValue item节点的link值 * @param descriptionValue item节点的description值 * @param pubdateValue item节点的pubdate值 * @return * */ @SuppressWarnings("unchecked") public void addElement(String fileName, Document document, String idValue, String titleValue, String linkValue, String descriptionValue, String pubdateValue) { /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; Element itemElement = channelElement.addElement(ITEM); itemElement.addAttribute(ITEMID, idValue); Element titleElement = itemElement.addElement(ITEMTITLE); titleElement.setText(titleValue); Element linkElement = itemElement.addElement(ITEMTLINK); linkElement.setText(linkValue); Element descriptionElement = itemElement.addElement(ITEMTDESCRIPTION); descriptionElement.setText(descriptionValue); Element pubDateElement = itemElement.addElement(ITEMTDPUBDATE); pubDateElement.setText(pubdateValue);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } } /** * 功能说明:初始化id,将id值设置为1-20 * @author fisher * @param fileName 文件路径 * @param nodeName 节点名称("//item/@id") * @return * */ @SuppressWarnings("unchecked") public void initId(String fileName, String nodeName) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } List list = document.selectNodes(nodeName); //创建一个迭代器.
Iterator iter = list.iterator(); int id = 1; while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); //设置属性的值
attribute.setValue("" + id); id++; } try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:初始化id,将id值设置为1-20 * @author fisher * @param fileName 文件路径 * @param nodeName 节点名称("//item/@id") * @return * */ @SuppressWarnings("unchecked") public void initId(String fileName, Document document, String nodeName) { //使用XPATH表达式从article元素中获得level节点列表
/* List list = document.selectNodes("//item/@id");*/ List list = document.selectNodes(nodeName); //创建一个迭代器.
Iterator iter = list.iterator(); int id = 1; while (iter.hasNext()) { //获得level节点的属性
Attribute attribute = (Attribute) iter.next(); //设置属性的值
attribute.setValue("" + id); id++; } try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:修改节点ID * @author fisher * @param fileName 文件路径 * @param nodeName 节点名称("//item/@id") * @param newid 新ID值 * @param oldid 旧ID值 * @return * */ @SuppressWarnings("unchecked") public void modifXMLByNodeId(String fileName, String nodeName, String newid, String oldid) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } //使用XPATH表达式从article元素中获得level节点列表
/* List list = document.selectNodes("//item/@id");*/ List list = document.selectNodes(nodeName); //创建一个迭代器.
Iterator iter = list.iterator(); while (iter.hasNext()) { //获得level节点的属性
Attribute attribute = (Attribute) iter.next(); if (attribute.getValue().equals(oldid)) //设置属性的值
attribute.setValue(newid); } try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:修改节点ID * @author fisher * @param fileName 文件路径 * @param document 文档 * @param nodeName 节点名称("//item/@id") * @param newid 新ID值 * @param oldid 旧ID值 * @return * */ @SuppressWarnings("unchecked") public void modifXMLByNodeId(String fileName, Document document, String nodeName, String newid, String oldid) { //使用XPATH表达式从article元素中获得level节点列表
/* List list = document.selectNodes("//item/@id");*/ List list = document.selectNodes(nodeName); //创建一个迭代器.
Iterator iter = list.iterator(); while (iter.hasNext()) { //获得level节点的属性
Attribute attribute = (Attribute) iter.next(); if (attribute.getValue().equals(oldid)) //设置属性的值
attribute.setValue(newid); } try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:移除第一个Item节点 * @author fisher * @param fileName 文件路径 * @return * */ public void removeFirstItem(String fileName) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; xmlDom4J.removeFirstNode(channelElement, ITEM, ITEMID);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:移除第一个Item节点 * @author fisher * @param fileName 文件路径 * @param document 文档 * @return * */ public void removeFirstItem(String fileName, Document document) { /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; xmlDom4J.removeFirstNode(channelElement, ITEM, ITEMID);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } } /** * 功能说明:通过节点ID移除Item节点 * @author fisher * @param fileName 文件路径 * @param document 文档 * @param idValue ID值 * @return * */ public void removeItemById(String fileName, Document document, String idValue) { /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; xmlDom4J.removeNode(channelElement, ITEM, ITEMID, idValue);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } } /** * 功能说明:通过节点ID移除Item节点 * @author fisher * @param fileName 文件路径 * @param idValue ID值 * @return * */ public void removeItemById(String fileName, String idValue) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return; xmlDom4J.removeNode(channelElement, ITEM, ITEMID, idValue);
try { xmlDom4J.writeToXml(fileName, document, Encoding); } catch (IOException e) { LOG.error("write to xml fail..."); LOG.error(e.getMessage()); } }
/** * 功能说明:获取节点个数 * @author fisher * @param fileName 文件路径 * @return * */ public int getItemNum(String fileName) { Document document = null; try { document = xmlDom4J.getDocument(fileName); } catch (DocumentException e1) { LOG.error("get document fail..."); LOG.error(e1.getMessage()); } /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return 0; return xmlDom4J.nodeNum(channelElement, ITEM); } /** * 功能说明:获取节点个数 * @author fisher * @param document 文档 * @return * */ public int getItemNum(Document document) { /**获取channel节点**/ Element channelElement = xmlDom4J.getNode(document, "//" + CHANNEL); if(channelElement == null) return 0; return xmlDom4J.nodeNum(channelElement, ITEM, ITEMID); } public static void main(String[] args) { NoteRssXml noteRssXml = new NoteRssXml(); /*noteRssXml.createDocument("D:/test.xml"); noteRssXml.addElement("D:/test.xml", "1", "标题3", "", "通知内容1", "发布时间1(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "2", "标题3", "", "通知内容2", "发布时间2(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "3", "标题3", "", "通知内容3", "发布时间3(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "4", "标题3", "", "通知内容2", "发布时间2(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "5", "标题3", "", "通知内容3", "发布时间3(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "6", "标题3", "", "通知内容2", "发布时间2(yyyy-MM-dd hh:mm:ss)"); noteRssXml.addElement("D:/test.xml", "7", "标题3", "", "通知内容3", "发布时间3(yyyy-MM-dd hh:mm:ss)"); noteRssXml.removeItemById("D:/test.xml", "1"); noteRssXml.removeFirstItem("D:/test.xml"); noteRssXml.addElement("D:/test.xml", "9", "标题3", "1", "通知内容3", "发布时间3(yyyy-MM-dd hh:mm:ss)"); noteRssXml.modifXMLByNodeId("D:/test.xml", "//item/@id", "10", "9");*/ noteRssXml.initId("D:/test.xml", "//item/@id"); System.out.println(noteRssXml.getItemNum("D:/test.xml")); } }
|