XML数据读取——Digester简单使用
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-VirgoArt,www.cnblogs.com
一、POM
<dependency><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId><version>2.1</version> </dependency>
二、XML文件准备
<?xml version="1.0" encoding="UTF-8"?> <webTemplate-methods><method><name>toListJSP</name><id>toListJSP</id><discribe>列表页面跳转Controller</discribe><jimport>org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;</jimport><jbean></jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/to${className?cap_first}ListJSP",method =RequestMethod.GET)public String to${className?cap_first}ListJSP(){return "${className}/${className}ListJSP";}]]></body></method><method><name>searchListMethod</name><id>searchListMethod</id><discribe>列表数据查询Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/searchListMethod", method = RequestMethod.GET)@ResponseBodypublic Map<String, Object> search${className?cap_first}ListMethod(){Map<String , Object> map = new HashMap<String , Object>();map.put("list",${className}Service.queryAll());return map;}]]></body></method><method><name>toAddJSP</name><id>toAddJSP</id><discribe>数据添加页面跳转Controller</discribe><jimport>org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;</jimport><jbean></jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/toAdd${className?cap_first}JSP",method = RequestMethod.GET)public String toAdd${className?cap_first}JSP(){return "${className}/${className}AddJSP";}]]></body></method><method><name>addMethod</name><id>addMethod</id><discribe>数据添加处理Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.RequestBody;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/add${className?cap_first}Method",method =RequestMethod.POST)@ResponseBodypublic Map<String, Object> add${className?cap_first}Method(@RequestBody ${packagePath}.entity.${className?cap_first} ${className}){Map <String ,Object> map =new HashMap<String ,Object>();int flag = ${className}Service.add(${className});if(flag>0){map.put("msg","SUCCESS");}else{map.put("msg","FAILED");}return map;}]]></body></method><method><name>toUpdateJSP</name><id>toUpdateJSP</id><discribe>数据更新页面跳转Controller</discribe><jimport>org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;</jimport><jbean></jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/toUpdate${className?cap_first}JSP",method =RequestMethod.GET)public String toUpdate${className?cap_first}JSP(){return "${className}/${className}UpdateJSP";}]]></body></method><method><name>queryByIdMethod</name><id>queryByIdMethod</id><discribe>数据更新页面数据获取Controller</discribe><jimport>org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/query${className?cap_first}ByIdMethod",method =RequestMethod.GET)@ResponseBodypublic Map<String,Object> query${className?cap_first}ByIdMethod(Integer id){Map <String ,Object> map = new HashMap<String, Object>();map.put("data",${className}Service.queryById(id));return map;}]]></body></method><method><name>updateMethod</name><id>updateMethod</id><discribe>数据更新处理Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.RequestBody;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/update${className?cap_first}Method",method =RequestMethod.POST)@ResponseBodypublic Map<String, Object> update${className?cap_first}Method(@RequestBody ${packagePath}.entity.${className?cap_first} ${className}){Map <String ,Object> map = new HashMap<String, Object>();int flag =${className}Service.update(${className});if(flag>0){map.put("msg","SUCCESS");}else{map.put("msg","FAILED");}return map;}]]></body></method><method><name>deleteMethod</name><id>deleteMethod</id><discribe>数据删除处理Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.RequestParam;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/delete${className?cap_first}Method",method =RequestMethod.GET)@ResponseBodypublic Map<String, Object> delete${className?cap_first}Method(@RequestParam String id){Map <String ,Object> map = new HashMap<String ,Object>();int flag = ${className}Service.delete(Integer.parseInt(id));if(flag>0){map.put("msg","SUCCESS");}else{map.put("msg","FAILED");}return map;}]]></body></method><method><name>deletesMethod</name><id>deletesMethod</id><discribe>数据批量删除处理Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.RequestParam;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/deletes${className?cap_first}Method",method =RequestMethod.GET)@ResponseBodypublic Map<String, Object> deletes${className?cap_first}Method(@RequestParam List<Integer> ids){Map <String ,Object> map = new HashMap<String ,Object>();int flag = ${className}Service.deletes(ids);map.put("msg","SUCCESS");return map;}]]></body></method><method><name>fileDownLoadMethod</name><id>fileDownLoadMethod</id><discribe>文件下载Controller</discribe><jimport>java.io.FileInputStream;java.io.IOException;java.io.InputStream;java.io.OutputStream;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;javax.servlet.http.HttpServletRequest;javax.servlet.http.HttpServletResponse;org.springframework.web.context.ContextLoader;cn.com.panji.common.util.FileUtils;</jimport><jbean></jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value ="/${className}/FileDownLoadMethod",method =RequestMethod.GET)public void fileDownLoadMethod(HttpServletRequest request, HttpServletResponse response) {String filePath = (String)request.getParameter("filepath");String fileName = (String)request.getParameter("filename");response.setContentType(ContextLoader.getCurrentWebApplicationContext().getServletContext().getMimeType(fileName));response.setHeader("Content-Disposition","attachment;filename*=utf-8'zh_cn'" + fileName);try {InputStream in =new FileInputStream(filePath);OutputStream out = response.getOutputStream();int b;while ((b = in.read()) != -1) {out.write(b);}in.close();out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}]]></body></method><method><name>fileUpLoadMethod</name><id>fileUpLoadMethod</id><discribe>文件上传Controller</discribe><jimport>java.io.File;java.util.HashMap;java.util.Map;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.RequestParam;javax.servlet.http.HttpServletRequest;javax.servlet.http.HttpServletResponse;org.springframework.web.multipart.MultipartFile;cn.com.panji.common.util.FileUtils;</jimport><jbean></jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value ="/${className?cap_first}/FileUpLoadMethod",method= RequestMethod.GET)@ResponseBodypublic Map<String ,Object>fileUpLoadMethod(HttpServletRequest request,HttpServletResponse response,@RequestParam("file") MultipartFile file) {Map<String ,Object> map = new HashMap<String ,Object>();File saveFile =FileUtils.saveFile(file, FileUtils.getRootPath(this) + "/loadFile/");if(null!=saveFile){map.put("msg","SUCCESS");}else{map.put("msg","FAILED");}return map;}]]></body></method><method><name>searchByIdMethod</name><id>searchByIdMethod</id><discribe>数据查询--By Id--Controller</discribe><jimport>java.util.Map;java.util.HashMap;org.springframework.web.bind.annotation.RequestMapping;org.springframework.web.bind.annotation.RequestMethod;org.springframework.web.bind.annotation.ResponseBody;</jimport><jbean>private ${packagePath}.service.${className?cap_first}Service ${className}Service;</jbean><comment>/*** **/</comment><body><![CDATA[ @RequestMapping(value = "/searchByIdMethod", method = RequestMethod.GET)@ResponseBodypublic Map<String, Object> search${className?cap_first}ByIdMethod(@RequestParam String id){Map<String , Object> map = new HashMap();map.put("data",${className}Service.queryById(Integer.parseInt(id)));return map;}]]></body></method> </webTemplate-methods>
View Code
三、创建映射对象
package cn.com.panji.common.resourcemanager.bean.web;import java.util.ArrayList; import java.util.List;import lombok.Data;/** Web资源文件方法集合* * @author */ @Data public class WebTemplateMethods {private List<WebTemplateMethod> methods = new ArrayList<>();public List<WebTemplateMethod> getMethods() {return methods;}public void setMethods(List<WebTemplateMethod> methods) {this.methods = methods;}public void addWebTemplateMethod(WebTemplateMethod method) {methods.add(method);} }
集合类
package cn.com.panji.common.resourcemanager.bean.web;import java.util.Arrays; import java.util.List;import cn.com.panji.common.util.StringUtils; import lombok.Data;/** Web资源文件方法实体* * @author */ @Data public class WebTemplateMethod {private String name;private String discribe;private String id;private String comment;private String jimport;private String jbean;private String body;public String getBody() {// body.replace("<", "<");// body.replace(">", ">");return body;}public void setBody(String body) {this.body = body;}public String getDiscribe() {return discribe;}public void setDiscribe(String discribe) {this.discribe = discribe;}public String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getJimport() {return jimport;}public List<String> getJimportList() {jimport = StringUtils.removeAllBlank(jimport);return Arrays.asList(jimport.split(";"));}public void setJimport(String jimport) {this.jimport = jimport;}public String getJbean() {return jbean;}public List<String> getJbeanList() {jbean = StringUtils.trim(jbean);return Arrays.asList(jbean.split(";"));}public void setJbean(String jbean) {this.jbean = jbean;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
封装类
四、Digester解析配置与应用
package cn.com.panji.common.resourcemanager.api.impl;import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map;import org.apache.commons.digester.Digester; import org.xml.sax.SAXException;import cn.com.panji.common.resourcemanager.ResourceEnum; import cn.com.panji.common.resourcemanager.api.TemplateXMLReader; import cn.com.panji.common.resourcemanager.bean.web.WebTemplateMethod; import cn.com.panji.common.resourcemanager.bean.web.WebTemplateMethods;public class WebTemplateXMLReader extends TemplateXMLReader {static final String WEB_TEMPLEATE_XML_PATH = "resourceTemplates/"+ ResourceEnum.XML_TEMPLATE_WEB.getResourceTemplatePath() + ".xml";private WebTemplateMethods methods = null;private WebTemplateXMLReader() {configDigester();parse();}@Overridepublic void configDigester() {this.digester = new Digester();this.digester.setValidating(false);// 配置映射this.digester.addObjectCreate("webTemplate-methods", WebTemplateMethods.class);this.digester.addObjectCreate("webTemplate-methods/method", WebTemplateMethod.class);this.digester.addBeanPropertySetter("webTemplate-methods/method/name", "name");this.digester.addBeanPropertySetter("webTemplate-methods/method/id", "id");this.digester.addBeanPropertySetter("webTemplate-methods/method/discribe", "discribe");this.digester.addBeanPropertySetter("webTemplate-methods/method/jimport", "jimport");this.digester.addBeanPropertySetter("webTemplate-methods/method/jbean", "jbean");this.digester.addBeanPropertySetter("webTemplate-methods/method/comment", "comment");this.digester.addBeanPropertySetter("webTemplate-methods/method/body", "body");this.digester.addSetNext("webTemplate-methods/method", "addWebTemplateMethod");}@Overridepublic void parse() {try {methods = (WebTemplateMethods) this.digester.parse(this.getClass().getClassLoader().getResourceAsStream(WEB_TEMPLEATE_XML_PATH));} catch (IOException | SAXException e) {e.printStackTrace();}}@Overridepublic WebTemplateMethod findResource(String id) {for (WebTemplateMethod m : methods.getMethods()) {if (id.equals(m.getId())) {return m;}}return null;}@Overridepublic Object findResourceMap(String[] ids) {Map<String, WebTemplateMethod> map = new HashMap<>();List<String> idList = Arrays.asList(ids);for (WebTemplateMethod m : methods.getMethods()) {if (idList.contains(m.getId())) {map.put(m.getId(), m);}}return map;}@Overridepublic Object findResourceList(String[] ids) {List<WebTemplateMethod> list = new ArrayList<WebTemplateMethod>();List<String> idList = Arrays.asList(ids);for (WebTemplateMethod m : methods.getMethods()) {if (idList.contains(m.getId())) {list.add(m);}}return list;}public static TemplateXMLReader getInstance() {return new WebTemplateXMLReader();} }
View Code
WebTemplateMethod web = (WebTemplateMethod) TemplateReaderFactory.getTemplateXMLReaderInstance(ResourceEnum.XML_TEMPLATE_WEB).findResource("updateMethod");
五、解释及注意事项
1.XML结构应与映射对象层次结构相同,解析时,可解析到最末端元素,关于List数据,使用事件驱动器进行反射,当扫描到元素时,调动相应的Add方法。
2.Digester使用反射Setter方式进行属性注入,需要保证存在有效的Setter方法。
3.项目中可用该方式实现更加清晰的项目参数配置文件以及解析。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>关于节点属性数据获取等内容后续更新
转载于:https://www.cnblogs.com/virgoart/p/10481611.html
XML数据读取——Digester简单使用相关推荐
- XML数据读取方式性能比较(一)
几个月来,疑被SOA,一直在和XML操作打交道,SQL差不多又忘光了.现在已经知道,至少有四种常用人XML数据操作方式(好像Java差不多),不过还没有实际比较过这些方式各有哪些特点或优劣.正好看到网 ...
- R语言学习:数据读取以及简单运算
这是去年业余时间学到的R语言基础知识,打算入门的朋友可以进来看看,大佬请绕道. 这一章记录的是关于R语言数据的读取和一些简单的运算方法. 如有疑问,欢迎留言说明. 数组的基础知识 判断是否是数组is. ...
- XML数据读取方式性能比较(一) (转)
地址:http://www.cnblogs.com/XmNotes/archive/2010/08/12/1796162.html 转载于:https://www.cnblogs.com/Music/ ...
- Unity 基础 之 xml 使用 Office Excel 轻松编辑保存 xml 数据,并解析读取数据
Unity 基础 之 xml 使用 Office Excel 轻松编辑保存 xml 数据 目录 Unity 基础 之 xml 使用 Office Excel 轻松编辑保存 xml 数据 一.简单介 ...
- 简单使用SAXReader解析xml数据
之前的工作中,一直是使用json格式的数据进行数据传输.很少会接触到xml格式的数据.不过因为工作需求,在对接其他产品的接口时,偶尔会遇到需要使用xml格式数据的情况,所以,也得学学如何解析xml.不 ...
- 以对象的方式来访问xml数据表(二)
为什么要以对象的方式来访问xml数据表? 还记得,自己是在一次完成师兄布置的任务时接触到了xml,那时候需要用xml来作为数据文件,保存一个简单的图书管理系统的数据.于是就知道了,可以用xml文件来保 ...
- php xmlreader 读xml,PHP中使用xmlreader读取xml数据示例
这篇文章主要介绍了PHP中使用xmlreader读取xml数据示例,本文示例相对简单,只包含了一个读取功能,需要的朋友可以参考下 有一个XML文件,,内容如下: 复制代码 代码如下: Simpsons ...
- Extjs的数据读取器store和后台返回类型简单解析
工作中用到了Extjs,从后台获取数据的时候,用到了extjs自己的Ext.data.store方法,然后封装了ExtGridReturn方法, 目的:前台用到Ext.data.store读取从后台传 ...
- python 读取excel格式xml,读取xml格式的xls文件、解析其中数据
1.python 读取excel格式xml,解析其中数据 当excel文件的格式是xml的时候,window系统是可以正常打开的,但是使用pandas直接读取则会报错,原因就是现在已经是xml文件了, ...
最新文章
- 不经历风雨,怎么能见彩虹!马克斯与我的不解之缘!
- 数据库MYSQL学习系列三
- pybind11传输文件
- 支持python开发的环境有哪些变化_Python开发实践:打造完美的项目工程环境
- 点击按钮创建一个表格 点击按钮创建一个表格 权限选择 元素的value属性操作
- phpcmsV9栏目内文章批量移动后,新的内容页模板不生效 -分析篇
- linux计划任务30秒,Linux 计划任务(at batch crontab anacron)
- maven install 安装项目问题总结An unknown compilation problem occurred
- html语言标记滚动字幕,网页滚动文字的制作HTML代码
- ssh连接远程服务器
- windows 10 安装EasyDarwin流服务器推流、本地存储
- Python3,csvkit功能竟如此强大,不仅可以转换csv格式文件,还能进行数据处理和分析。
- host文件的用途和用法
- 手机群控软件的运行环境台式机/HUB配置分享
- 一个简洁的倒计时shell 脚本
- 自爆神舟电脑为什么便宜,明眼人可以看懂。
- IDEA断点无效(干货)
- Google C++ 编码规范(中文版)
- 今年冬天有点冷(2)
- 英语----倒装句(上):完全倒装
热门文章
- windows 下执行mysql脚本_Windows下批处理执行MySQL脚本文件
- 审批流_想做流程审批的开发?带你玩转Activity工作流(一)
- Oracle大的存储层次体系,Oracle 数据库中的逻辑存储层次体系
- java图片转换成base64_Java将图片转换成Base64字符串
- html语言 section type,HTML5中div、article、section的区别及使用介绍
- mysql show schema_快速入门 · xiaoboluo768/mysql-system-schema Wiki · GitHub
- 模型预测控制c语言程序_基于数据驱动的模型预测控制
- 超标量体系结构_CPU体系结构以及指令流水原理
- rust房子 如何拆除_“一户多宅”将陆续清查!违规如何处理?
- 天翼云从业认证课后习题(3.3天翼云网络产品)