动态修改xml文件
程序员文章站
2022-06-06 13:08:24
...
接上篇文章动态修改properties文件,中的FileWacthdog.java作为父类,代码如下:
import org.apache.commons.digester.Digester; /** * <p> * Info: 动态加载collectionsFavourableActivity.xml文件,<br /> * 该文件使用 common-digester 库解析因此解析的文件要符合digester库要求的格式。 * </p> * <p> * Date: 2014-4-28 14:22:54 * </p> * @author zhangyq * */ public class CollectionsFavourableActiDynamicLoading { //------- 实体类变量 ---------------------------------- private static CollectionsFavourableActivity activity; public static CollectionsFavourableActivity getActivity() { return activity; } /** * 根据传递的文件配置并加载该文件 * @param configFilename 要解析文件的绝对路径 * @param delay 检验文件的间隔时间段 */ public static void configureAndLoad(String configFilename, long delay) { PropertyWatchdog pdog = new PropertyWatchdog(configFilename); pdog.setDelay(delay); pdog.start(); } //-------------------------------------- 静态内部类定义 -------------------------- /** * Info: 动态读取配置文件的信息 * @author zhangyq */ static class PropertyWatchdog extends FileWatchdog { /** * 构造函数 * @param configFileName 绝对路径 */ PropertyWatchdog(String filename){ super(filename); } @Override protected void doOnChange() { try { Digester digester = new Digester(); digester.addObjectCreate("CollectionsFavourableActivity", CollectionsFavourableActivity.class); digester.addSetProperties("CollectionsFavourableActivity"); activity = (CollectionsFavourableActivity) digester.parse(file); } catch (Exception e) { return; } } } //--------------------------------------end 静态内部类定义 end-------------------------- }
使用Digester库解析xml文件,上述解析的xml文件为:
<?xml version="1.0" encoding="UTF-8"?> <CollectionsFavourableActivity avtivitySatrtDate="2014-05-10 00:00:00" avtivityEndDate="2014-06-10 23:59:59" upperLimit="0" lowerLimit="1000" maxCountEveryMounth="3"> </CollectionsFavourableActivity>
Digester库解析后生成的对象类为:
public class CollectionsFavourableActivity { //----------------------------------------------- Instance Constants ---------------------- private String avtivitySatrtDate;//活动开始日期 private String avtivityEndDate;//活动结束日期 private Double upperLimit;//上限金额 private Double lowerLimit;//下限金额 private int maxCountEveryMounth;//每月最多优惠笔数 /** * 空的构造函数 */ public CollectionsFavourableActivity(){ super(); } /** * 一档 1000元(含)-5000元 10 10 * 二档 5001-10000元 50 50 * 三档 10001-50000(含)元 80 80 * 四档 50001-200000含)元 120 120 * 五档 200001元以上 150 150 * Info: 根据支付金额的及优惠笔数获得优惠后的金额 * @param orderPrice 订单金额 * @return 计算后的金额 */ public static double cal(double orderPrice){ double result = 0.0; if (orderPrice < 1000 && orderPrice > 0) { result = orderPrice; } else if (orderPrice >=1000 && orderPrice <= 5000) { result = orderPrice - 10; }else if (orderPrice > 5000 && orderPrice <= 10000) { result = orderPrice - 50; }else if (orderPrice > 10000 && orderPrice <= 50000) { result = orderPrice - 80; }else if (orderPrice > 50000 && orderPrice <= 200000) { result = orderPrice - 120; }else if (orderPrice > 200000) { result = orderPrice - 150; } return result ; } @Override public String toString() { return "{开始日期:"+avtivitySatrtDate+" 结束日期:"+avtivityEndDate+"\r\n"+ "上限金额:"+upperLimit.doubleValue()+" 下限金额:"+lowerLimit.doubleValue()+"\r\n"+ " 每月最多优惠笔数:"+maxCountEveryMounth+"}"; } //----------------------------------- start getter setter ----------------------------- public Double getUpperLimit() { return upperLimit; } public void setUpperLimit(Double upperLimit) { this.upperLimit = upperLimit; } public Double getLowerLimit() { return lowerLimit; } public void setLowerLimit(Double lowerLimit) { this.lowerLimit = lowerLimit; } public int getMaxCountEveryMounth() { return maxCountEveryMounth; } public void setMaxCountEveryMounth(int maxCountEveryMounth) { this.maxCountEveryMounth = maxCountEveryMounth; } public String getAvtivitySatrtDate() { return avtivitySatrtDate; } public void setAvtivitySatrtDate(String avtivitySatrtDate) { this.avtivitySatrtDate = avtivitySatrtDate; } public String getAvtivityEndDate() { return avtivityEndDate; } public void setAvtivityEndDate(String avtivityEndDate) { this.avtivityEndDate = avtivityEndDate; } //----------------------------------- end getter setter ----------------------------- }
测试使用:
CollectionsFavourableActiDynamicLoading.configureAndLoad(configFilename, delay); CollectionsFavourableActivity activity = CollectionsFavourableActiDynamicLoading.getActivity();
上一篇: 美容养颜水果有哪些?吃它让你立马变美!
下一篇: 别往坏处想