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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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)
- @ResponseBody
- public 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>
三、创建映射对象
- 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();
- }
- @Override
- public 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");
- }
- @Override
- public void parse() {
- try {
- methods = (WebTemplateMethods) this.digester
- .parse(this.getClass().getClassLoader().getResourceAsStream(WEB_TEMPLEATE_XML_PATH));
- } catch (IOException | SAXException e) {
- e.printStackTrace();
- }
- }
- @Override
- public WebTemplateMethod findResource(String id) {
- for (WebTemplateMethod m : methods.getMethods()) {
- if (id.equals(m.getId())) {
- return m;
- }
- }
- return null;
- }
- @Override
- public 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;
- }
- @Override
- public 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();
- }
- }
- WebTemplateMethod web = (WebTemplateMethod) TemplateReaderFactory.getTemplateXMLReaderInstance(ResourceEnum.XML_TEMPLATE_WEB).findResource("updateMethod");
五、解释及注意事项
1.XML结构应与映射对象层次结构相同,解析时,可解析到最末端元素,关于List数据,使用事件驱动器进行反射,当扫描到元素时,调动相应的Add方法。
2.Digester使用反射Setter方式进行属性注入,需要保证存在有效的Setter方法。
3.项目中可用该方式实现更加清晰的项目参数配置文件以及解析。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>关于节点属性数据获取等内容后续更新
XML数据读取——Digester简单使用的更多相关文章
- XML数据读取方式性能比较(一)
原文:XML数据读取方式性能比较(一) 几个月来,疑被SOA,一直在和XML操作打交道,SQL差不多又忘光了.现在已经知道,至少有四种常用人XML数据操作方式(好像Java差不多),不过还没有实际比较 ...
- 以对象的方式来访问xml数据表(二)
为什么要以对象的方式来访问xml数据表? 还记得,自己是在一次完成师兄布置的任务时接触到了xml,那时候需要用xml来作为数据文件,保存一个简单的图书管理系统的数据.于是就知道了,可以用xml文件来保 ...
- c#调用JAVA的Webservice处理XML数据及批量轮询的实现方法
前段时间做一个调用外单位WEBSERVICE的项目,项目完成的功能其实很简单,就是我们单位有很多车友会员,我们想对他们提供车辆违章信息告之服务!我们这边交警部门给我们开放了WS的接口,我们就是想通过这 ...
- 使用dom4j中SAXReader解析xml数据
public ApiConfig(String configFilePath) throws DocumentException{ SAXReader reader = new SAXReader() ...
- Extjs的数据读取器store和后台返回类型简单解析
工作中用到了Extjs,从后台获取数据的时候,用到了extjs自己的Ext.data.store方法,然后封装了ExtGridReturn方法, 目的:前台用到Ext.data.store读取从后台传 ...
- InnerException 消息是“反序列化对象 属于类型 *** 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。(注意细节)
WEB站点在调用我们WCF服务的时候,只要传入的参数过长,就报如下错误: 格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: formD ...
- C#基础笔记---浅谈XML读取以及简单的ORM实现
背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...
- C#基础---浅谈XML读取以及简单的ORM实现
背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...
- XML教程、语法手册、数据读取方式大全
XML简单易懂教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 XML --数据格式的写法 二 Re ...
随机推荐
- ubuntu下adb的使用以及开启黑域
ubuntu使用adb开启黑域 刷了原生后经好友推荐, 黑域对于App的管控效果还是很不错的 adb的安装 此处顺带着就把fastboot也安装了 sudo apt update sudo apt i ...
- luogu5290 春节十二响
题目链接 思路 先考虑一条链的情况怎么做. 因为只有两个子树,并且两个子树都是链.所以可以把这两条链找出来,然后\(sort\)一下.合并起来. 然后推广到树上 对于每一棵树都可以按照和上面同样的方法 ...
- C++预编译头文件 – stdafx.h
预编译头文件的由来 也许请教了别的高手之后,他们会告诉你,这是预编译头,必须包含.可是,这到底是为什么呢?预编译头有什么用呢? 咱们从头文件的编译原理讲起.其实头文件并不神秘,其在编译时的作用,就是把 ...
- NPOI读取excel表,如果有公式取出的是公式,想要取数字怎么办?
public static DataTable Import(string strFileName) { DataTable dt = new DataTable(); HSSFWorkbook hs ...
- fhq treap
学了一下,好像明白了(背下来了) 不想写main函数了 PS:这个比treap好写(私以为) #include<bits/stdc++.h> using namespace std; in ...
- select中option的onclick事件失效
html: <select id="pageSelect"> <option value="1" selected onclick=" ...
- 二、初始化superset
上一步成功安装了superset, 1.初始化数据 创建命令 #创建管理员账号fabmanager create-admin --app superset#cd到superset 根目录参考路径 C: ...
- easyui 菜单树搜索
//树形菜单搜索方法 function searchTree(treeObj,parentNode,searchCon){ var children; for(var ...
- Android OS的image文件组成
Android OS由以下image文件组成: 1)Bootloader ---在设备启动时开始加载Boot image 2)Boot image ---Kernel 和 RAMdisk 3)Syst ...
- 关于npm镜像,发布,内网搭建等
npm config set registry http://registry.npmjs.org npm config set registry https://registry.npm.taoba ...