日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

XML数据读取——Digester简单使用

發布時間:2025/3/19 asp.net 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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("&lt;", "<");// body.replace("&gt;", ">");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简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。