介绍

之前写过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文件的更多相关文章

  1. MyBatis 入门到精通(二) SQL语句映射XML文件

    MyBatis 真正强大之处就在这些映射语句,也就是它的魔力所在.对于它的强大功能,SQL 映射文件的配置却非常简单. 如果您比较SQL 映射文件配置与JDBC 代码,您很快可以发现,使用SQL 映射 ...

  2. MyBatis Spring整合配置映射接口类与映射xml文件

    本文转自http://blog.csdn.net/zht666/article/details/38706083 Spring整合MyBatis使用到了mybatis-spring,在配置mybati ...

  3. hibernate映射xml文件配置之一对多,多对多

    一对多配置 [1]班级和学生模型 --->班级可容纳多个学生 --->学生只能属于一个班级 [2]一对多配置中的关系维护(inverse) --->一端放弃关系的维护 ---> ...

  4. MyBatis框架的使用及源码分析(四) 解析Mapper接口映射xml文件

    在<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 一文中,我们知道mybat ...

  5. 映射Xml文件中的数据到JavaBean中

    使用Java原生的javax.xml.bind包下的JAXBContext将一个Xml文件中的数据映射到一个JavaBean中 import java.io.File; import java.io. ...

  6. Mybatis映射.xml文件报错

    MyBatis框架里面,在dao层进行测试,控制台显示错误是:必须为元素类型 "delete" 声明属性 "resultType" 相应的.xml文件的sql语 ...

  7. 用js(JavaScript-jQuery)解析XML文件 无法成功 获得XML对象,字符串一些心得

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/7822962.html 解析XML文件遇到的问题 今天秦博士叫我解析一下XML文件,将里面的所有 ...

  8. java实现xml文件读取并保存到对象

    首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...

  9. Java读取CSV和XML文件方法

    游戏开发中,读取策划给的配置表是必不可少的,我在之前公司,策划给的是xml表来读取,现在公司策划给的是CSV表来读取,其实大同小异,也并不是什么难点,我就简单分享下Java如何读取XML文件和CSV文 ...

随机推荐

  1. amazeui学习笔记一(开始使用4)--Web App 相关

    amazeui学习笔记一(开始使用4)--Web App 相关 一.总结 1.桌面图标(Touch icon)解决方案:终极方案:link标签的rel和href属性: <link rel=&qu ...

  2. 机房收费 &amp; 廊院食堂

    做机房收费系统时.常常想这个一般用户指的是谁?我当初以为是学生......可能是被数据库中的student带跑偏了...... 事实上把我们的系统联系一下实际,就会非常easy想到一般用户指的是谁的位 ...

  3. 重新启动IIS不重启电脑

      有时候我们在WEB程序如:ASP,中无意中使用到了一个死循环,或者在测试 DLL组件时,挂了.这时候IIS就停止了响应,我们要继续我们的工作啊,重启IIS服务吧. 然而这个进程还在执行,Inter ...

  4. 区分json与jsonp

    JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不是一回事儿,下边简单区分概括一下: JSON是 ...

  5. windows下安装cmake

    windows下安装cmake 下载地址 download -> cmake-3.12.0-rc2-win64-x64.msi 安装 验证cmake --version

  6. 如何安装Python环境以及为Visual Studio 2012安装Python插件

    (一)首先,我机器上的开发环境安装的是Visual Studio 2012版本,系统为window7,64位,要安装的Python版本为python3.4-x64,双击安装包安装Python环境,需要 ...

  7. poj1679 The Unique MST(判定次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23180   Accepted: 8235 D ...

  8. Android onLoadFinished与onLoaderReset

    onLoadFinished 这个方法是在前面已创建的加载器已经完成其加载过程后被调用,这个方法保证会在应用到加载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...

  9. java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: student is not mapped

    Spring 5.0 +Jpa,使用@Query实现 自定义查询报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.a ...

  10. 【例题5-4 UVA - 156】Ananagrams

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个字符串如果每个字符按照升序排一下.假设他们能够互相变化. 则肯定是一样的. 根据这个东西,用一个map来判重就好. [错的次数] ...