介绍

之前写过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. 2. Dubbo和Zookeeper的关系

    转自:https://www.cnblogs.com/hirampeng/p/9540243.html Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用: ...

  2. GitHub上常用命令(工作中几乎每天用到的命令)

    查看自己当前分支 git branch 查看远程和本地分支 git branch -a 查看origin下的分支 git config -vv git config --lis 创建分支 git br ...

  3. Docker---(8)Docker启动Redis后访问不了

    原文:Docker---(8)Docker启动Redis后访问不了 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/ ...

  4. PatentTips - Resource partitioning and direct access utilizing hardware support for virtualization

    BACKGROUND The present disclosure relates to the resource management of virtual machine(s) using har ...

  5. Traveler Nobita (zoj 3456 最小生成树)

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  6. LeetCode_Construct Binary Tree from Preorder and Inorder Traversal

    一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...

  7. GOROOT,GOPATH,GOBIN,project

    GOROOT,GOPATH,GOBIN,project目录   我们接下来一个一个来看关于Go语言中的三个目录的详细解释先通过go env查看go的环境变量(我这里是mac的环境,所以可能和你的不同) ...

  8. 关于pcb铺铜

  9. postman--下载及使用入门

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  10. 具体解释。。设计模式5——DAO。。studying

    设计模式5--DAO ★ 场景和问题 在Java程序中,常常须要把数据持久化.也须要获取持久化的数据,可是在进行数据持久化的过程中面临诸多问题 (如:数据源不同.存储类型不同.供应商不同.訪问方式不同 ...