VO对象通过groovy模板映射XML文件
介绍
之前写过JAVA+XSLT相关的技术博客,近期研究了一个开源工具包org.codehaus.groovy,处理VO对象和XML文件映射很方便。
简言之:将VO对象中的属性(包含Collection, Map),通过groovy模板,映射XML文件。
Maven pom.xml
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.0</version>
</dependency>
VO类:
package shuai.study.groovy.demo.vo; import java.util.LinkedHashMap;
import java.util.Map; /**
* @author shengshu
*
*/
public class MeasurementVO {
private String dn = null;
private String measurementType = null; private Map<String, Integer> counterMap = new LinkedHashMap<String, Integer>(); public String getDn() {
return this.dn;
} public void setDn(String dn) {
this.dn = dn;
} public String getMeasurementType() {
return this.measurementType;
} public void setMeasurementType(String measurementType) {
this.measurementType = measurementType;
} public Map<String, Integer> getCounterMap() {
return this.counterMap;
} public void setCounterMap(Map<String, Integer> counterMap) {
this.counterMap = counterMap;
}
}
Collection类:
package shuai.study.groovy.demo.collection; import java.util.LinkedList;
import java.util.List; import shuai.study.groovy.demo.vo.MeasurementVO; /**
* @author shengshu
*
*/
public class MeasurementCollection {
List<MeasurementVO> MeasurementVoList = new LinkedList<MeasurementVO>(); public List<MeasurementVO> getMeasurementVoList() {
return this.MeasurementVoList;
} public void setMeasurementVoList(MeasurementVO measurementVO) {
this.MeasurementVoList.add(measurementVO);
}
}
groovy模板文件:
<?xml version="1.0"?>
<OMeS>
<PMSetup>
<PMMOResult>
<% def measurementVoList=measurementCollection.getMeasurementVoList();
for (measurementVo in measurementVoList) {
def dn=measurementVo.getDn();
def measurementType=measurementVo.getMeasurementType();%> <MO>
<DN>${dn}</DN>
</MO> <PMTarget measurementType="${measurementType}">
<% def counterMap=measurementVo.getCounterMap();
counterMap.each {
def counterKey=it.key;
def counterValue=it.value;%> <${counterKey}>${counterValue}</${counterKey}> <%}%>
</PMTarget> <%}%>
</PMMOResult>
</PMSetup>
</OMeS>
Transfer类:
package shuai.study.groovy.demo.transfer; import groovy.text.SimpleTemplateEngine;
import groovy.text.Template; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.io.IOUtils;
import org.codehaus.groovy.control.CompilationFailedException; import shuai.study.groovy.demo.collection.MeasurementCollection; /**
* @author shengshu
*
*/
public class MeasurementTransfer {
private static MeasurementTransfer measurementTransfer = null; private Template template = null; public static MeasurementTransfer getMeasurementTransfer() {
if (measurementTransfer == null) {
synchronized (MeasurementTransfer.class) {
if (measurementTransfer == null) {
measurementTransfer = new MeasurementTransfer();
}
}
} return measurementTransfer;
} private MeasurementTransfer() {
File measurementTemplateFile = new File("/demo/groovy/template/measurementTemplate.xml"); if (!measurementTemplateFile.exists()) {
throw new NullPointerException("Measurement template file " + measurementTemplateFile + " is null");
} try {
template = new SimpleTemplateEngine().createTemplate(measurementTemplateFile);
} catch (CompilationFailedException cfe) {
cfe.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
} public void transfer(MeasurementCollection measurementCollection, File outputFile) {
Map<String, MeasurementCollection> measurementCollectionMap = new HashMap<String, MeasurementCollection>();
measurementCollectionMap.put("measurementCollection", measurementCollection); Writer writer = null; try {
FileWriter fileWriter = new FileWriter(outputFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); writer = template.make(measurementCollectionMap).writeTo(bufferedWriter);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
IOUtils.closeQuietly(writer);
}
}
}
Executer启动类:
package shuai.study.groovy.demo; import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map; import org.apache.commons.io.FileUtils; import shuai.study.groovy.demo.collection.MeasurementCollection;
import shuai.study.groovy.demo.transfer.MeasurementTransfer;
import shuai.study.groovy.demo.vo.MeasurementVO; /**
* @author shengshu
*
*/
public class MeasurementExecuter { public static void main(String[] args) {
Map<String, Integer> counterMap = new LinkedHashMap<String, Integer>();
counterMap.put("nbrOfProvModify", 100);
counterMap.put("nbrOfProvCreate", 300);
counterMap.put("nbrOfProvDelete", 500); MeasurementVO measurementVO = new MeasurementVO();
measurementVO.setDn("PLMN-PLMN/IPS-1/CNODE-2/STYP-3");
measurementVO.setMeasurementType("PERFMGMT");
measurementVO.setCounterMap(counterMap); MeasurementCollection measurementCollection = new MeasurementCollection();
measurementCollection.setMeasurementVoList(measurementVO); File measurementOutputFile = new File("/demo/groovy/output/measurementOutput.xml"); try {
FileUtils.touch(measurementOutputFile);
} catch (IOException ioe) {
ioe.printStackTrace();
} MeasurementTransfer measurementTransfer = MeasurementTransfer.getMeasurementTransfer();
measurementTransfer.transfer(measurementCollection, measurementOutputFile);
}
}
Output执行输出文件:
<?xml version="1.0"? >
<OMeS>
<PMSetup>
<PMMOResult> <MO>
<DN>PLMN-PLMN/IPS-1/CNODE-2/STYP-3</DN>
</MO> <PMTarget measurementType="PERFMGMT"> <nbrOfProvModify>100</nbrOfProvModify> <nbrOfProvCreate>300</nbrOfProvCreate> <nbrOfProvDelete>500</nbrOfProvDelete> </PMTarget> </PMMOResult>
</PMSetup>
</OMeS>
VO对象通过groovy模板映射XML文件的更多相关文章
- MyBatis 入门到精通(二) SQL语句映射XML文件
MyBatis 真正强大之处就在这些映射语句,也就是它的魔力所在.对于它的强大功能,SQL 映射文件的配置却非常简单. 如果您比较SQL 映射文件配置与JDBC 代码,您很快可以发现,使用SQL 映射 ...
- MyBatis Spring整合配置映射接口类与映射xml文件
本文转自http://blog.csdn.net/zht666/article/details/38706083 Spring整合MyBatis使用到了mybatis-spring,在配置mybati ...
- hibernate映射xml文件配置之一对多,多对多
一对多配置 [1]班级和学生模型 --->班级可容纳多个学生 --->学生只能属于一个班级 [2]一对多配置中的关系维护(inverse) --->一端放弃关系的维护 ---> ...
- MyBatis框架的使用及源码分析(四) 解析Mapper接口映射xml文件
在<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 一文中,我们知道mybat ...
- 映射Xml文件中的数据到JavaBean中
使用Java原生的javax.xml.bind包下的JAXBContext将一个Xml文件中的数据映射到一个JavaBean中 import java.io.File; import java.io. ...
- Mybatis映射.xml文件报错
MyBatis框架里面,在dao层进行测试,控制台显示错误是:必须为元素类型 "delete" 声明属性 "resultType" 相应的.xml文件的sql语 ...
- 用js(JavaScript-jQuery)解析XML文件 无法成功 获得XML对象,字符串一些心得
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/7822962.html 解析XML文件遇到的问题 今天秦博士叫我解析一下XML文件,将里面的所有 ...
- java实现xml文件读取并保存到对象
首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...
- Java读取CSV和XML文件方法
游戏开发中,读取策划给的配置表是必不可少的,我在之前公司,策划给的是xml表来读取,现在公司策划给的是CSV表来读取,其实大同小异,也并不是什么难点,我就简单分享下Java如何读取XML文件和CSV文 ...
随机推荐
- HDU 5389 Zero Escape (MUT#8 dp优化)
[题目链接]:pid=5389">click here~~ [题目大意]: 题意: 给出n个人的id,有两个门,每一个门有一个标号,我们记作a和b,如今我们要将n个人分成两组,进入两个 ...
- Linux下常用的中文输入法平台有IBus、fcitx和scim
Linux下常用的中文输入法平台有IBus.fcitx和scim.scim现在维护滞后,不推荐使用. IBus ("Intelligent Input Bus") 是一个 输入法框 ...
- amazeui学习笔记一(开始使用1)--主干
amazeui学习笔记一(开始使用1)--主干 一.总结 1.英语:学好英语,编程轻松很多 2. layouts compatibility change log web app collection ...
- 位数(digits)的处理
主要针对:二进制表示法,以及十进制表示法: 1. 获取位数 已知该数 n 采用十进制进行表示 二进制形式的位数:⌊log2n⌋+1 十进制形式的位数:⌊log10n⌋+1 2. 截断(保留前/后 m ...
- JS错误记录 - 按左右箭头div移动、一串div跟着鼠标移动
本次练习错误总结: 1. div跟着用户操作而移动,首先必须要绝对定位,否则无法移动. 2. if条件语句里面是双等号,不是单等号(赋值). 3. 坐标值没有Right,只能offsetLeft 加减 ...
- P2P平台很赚钱么?
最近几年,搞P2P网贷和财富投资相关的金融周边公司,多了很多,楼下门店和电梯里的贷款小广告,真是多啊. 大家都去搞一件事的时候,很可能是大家都觉得这件事有利可图.但事实是,赚钱的总是少数,看到别人搞的 ...
- 扩展的方法:es6 安装模块builder
https://github.com/es-shims/es5-shim/ Image.png 检测浏览器可支持es5,不支持就扩展,做兼容: 扩展的方法: Image.png 取所有对象的键值: o ...
- struts2_7_Action类中方法的动态调用
(一)直接调用方法(不推荐使用) 1)Action类: private String savePath; public String getSavePath() { return savePath; ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- Qt开发程序在Windows 10应用须要管理员执行的解决思路
Qt开发程序在Windows 10应用须要管理员执行的解决思路 过了非常长的时间没有公布博客了.可是我依旧努力地开发Qt程序.眼下呢.我发现开发Qt程序在Windows 10上有一个怪现象--有些程序 ...