介绍

之前写过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. ubuntu-工作环境配置(van)

    我们平时用到的工具主要vim,retag_app,提交代码,以及一些常用命令,他们主要对应一下几个文件 1.vim ->.vimrc 2.代码提交->gitconfig 3.常用命令-&g ...

  2. P2P平台项目建设几点注意事项

    种选择 a.资金池,接入第三方支付,财付通,宝付等,比如一起好 b.第三方资金托管,易宝支付.汇付天下等 c.银行存管,资金放在银行 资金池政策风险,不得不考虑. 每一种模式,技术实现都有变化,需要考 ...

  3. 关于pcb铺铜

  4. SN74HC573ANSR 锁存器

    这是它的工作电压范围 这个是功能表,数电上曾经学过: 内部的框图,也是数电的知识:

  5. 2.Docker初体验【Docker每天5分钟】

    原文:2.Docker初体验[Docker每天5分钟] Docker给PaaS世界带来的“降维打击”,其实是提供了一种非常便利的打包机制.该机制打包了应用运行所需要的整个操作系统,从而保证了本地环境和 ...

  6. [D3] Margin Convention with D3 v4

    You can’t add axes to a chart if you don’t make room for them. To that end, the D3 community has ado ...

  7. Web--CSS控制页面(link与import方式差别)

        先了解: [1]         "Table"和"DIV"这两个网页元素诞生的目的不同,首先Table诞生的目的是为了存储数据,而DIV诞生的目的就是 ...

  8. html实现返回上一页的几种方法(javaScript:history.go(-1);)

    html实现返回上一页的几种方法(javaScript:history.go(-1);) 一.总结: 1.javaScript:history.go(-1); 二.方法 1.通过超链接返回到上一页 & ...

  9. UVA 11609 - Teams 组合、快速幂取模

    看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...

  10. 将App程序发布到苹果App Store

    发布iOS应用程序到App Store - 前期工作 要发布iOS应用程序到App Store首先需要一个iOS developer帐号,账号是收费的,$99美元/年.即便是免费应用也需要一个开发者账 ...