当前位置:网站首页>XML建模
XML建模
2022-07-05 20:46:00 【時宜】
目录
一、xml建模核心思想
xml建模的核心思想就是利用java面向对象的特性,用操作对象的方式操作xml。
二、xml建模的作用
1、节约资源
2、优化性能
3、更加便捷操作xml文件
三、建模案例
以下方config.xml为例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<config>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>
- 一个标签就是一个对象,在上方xml文件中有三个标签config、action、forward。所以我们需要三个实体类来进行建模
ConfigModel类
在集合中增加ActionModel对象,通过path找到对应的ActionModel对象
package com.zking.mymvc.framework;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> actionMap=new HashMap<String, ActionModel>();
/**
* 将ActionModel对象放到map集合
* @param forward
*/
public void put(ActionModel action) {
if(actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("Action path ="+ action.getPath()+" duplicate definition");
}
actionMap.put(action.getPath(), action);
}
/**
* 通过action中的path从map结合中取出对应的action对象 若path填错则抛出自定义异常未找到
* @param name
* @return
*/
public ActionModel find(String path) {
if(!actionMap.containsKey(path)) {
throw new ActionNotFoundException("Action path ="+ path +" not found");
}
return actionMap.get(path);
}
@Override
public String toString() {
return "ConfigModel [actionMap=" + actionMap + "]";
}
}
ActionModel类
在集合中增加ForwardModel对象,通过path找到对应的ForwardModel对象
package com.zking.mymvc.framework;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> forwardMap=new HashMap<String, ForwardModel>();
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Map<String, ForwardModel> getForwardMap() {
return forwardMap;
}
public void setForwardMap(Map<String, ForwardModel> forwardMap) {
this.forwardMap = forwardMap;
}
/**
* 将forwardModel对象放到map集合
* @param forward
*/
public void put(ForwardModel forward) {
if(forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("forward name= "+forward.getName()+"duplicate definition");
}
forwardMap.put(forward.getName(), forward);
}
/**
* 通过forward中的name从map结合中取出对应的forward对象 若name填错则抛出自定义异常未找到
* @param name
* @return
*/
public ForwardModel find(String name) {
if(!forwardMap.containsKey(name)) {
throw new ForwardNotFoundException("Forward name ="+name+" not found");
}
return forwardMap.get(name);
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
}
}
自定义异常
- action重复定义
package com.zking.mymvc.framework;
public class ActionDuplicateDefinitionException extends RuntimeException{
public ActionDuplicateDefinitionException() {
super();
}
/**
* 报错信息
* @param msg
*/
public ActionDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* 报错信息、报错原因
* @param msg
* @param c
*/
public ActionDuplicateDefinitionException(String msg,Throwable c) {
super(msg,c);
}
}
- action的path找不到
package com.zking.mymvc.framework;
public class ActionNotFoundException extends RuntimeException {
public ActionNotFoundException() {
super();
}
/**
* 报错信息
* @param msg
*/
public ActionNotFoundException(String msg) {
super(msg);
}
/**
* 报错信息、报错原因
* @param msg
* @param c
*/
public ActionNotFoundException(String msg,Throwable c) {
super(msg,c);
}
}
ForwardModel类
package com.zking.mymvc.framework;
public class ForwardModel {
private String name;
private String path;
private boolean redirect;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
public void setRedirect(String redirect) {
this.redirect = Boolean.valueOf(redirect);
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
自定义异常
- forward重复定义
package com.zking.mymvc.framework;
public class ForwardDuplicateDefinitionException extends RuntimeException{
public ForwardDuplicateDefinitionException() {
super();
}
/**
* 报错信息
* @param msg
*
*/
public ForwardDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* 报错信息、报错原因
* @param msg
* @param c
*/
public ForwardDuplicateDefinitionException(String msg,Throwable c) {
super(msg,c);
}
}
- forward中的name找不到
package com.zking.mymvc.framework;
public class ForwardNotFoundException extends RuntimeException {
public ForwardNotFoundException() {
super();
}
/**
* 报错信息
* @param msg
*/
public ForwardNotFoundException(String msg) {
super(msg);
}
/**
* 报错信息、报错原因
* @param msg
* @param c
*/
public ForwardNotFoundException(String msg,Throwable c) {
super(msg,c);
}
}
- 当三个都完成后,将他们装到ConfigModel中去,需要建一个ConfigFactory类
ConfigFactory类
package com.zking.mymvc.framework;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.XMLReader;
@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
private ConfigModelFactory() {
}
private static ConfigModel config=new ConfigModel();
//进行读取config.xml中的数据填充到模型中
static {
try {
//1.获取io流
InputStream in = XMLReader.class.getResourceAsStream("/config.xml");
//2.创建xml读取工具类SAXReader
SAXReader sax = new SAXReader();
//3.读取配置文件,获得Document对象
Document doc = sax.read(in);
//获取根元素
Element rootElement = doc.getRootElement();
//找到action节点
List<Element> actions = rootElement.selectNodes("action");
for(Element e: actions) {
//获取属性值
String path = e.attributeValue("path");
String type = e.attributeValue("type");
//实例化ActionModel对象
ActionModel action = new ActionModel();
//将属性值放到ActionModel中
action.setPath(path);
action.setType(type);
//找到forward节点
List<Element> forwards = e.selectNodes("forward");
for(Element f: forwards) {
//获取属性值
String name = f.attributeValue("name");
String fpath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
//实例化ForwardModel对象
ForwardModel forward = new ForwardModel();
//将属性值放入ForwardModel对象中去
forward.setName(name);
forward.setPath(fpath);
forward.setRedirect(redirect);
//将ForwardModel放到ActionModel集合中去
action.put(forward);
}
//将ActionModel放到集合中去ConfigModel
config.put(action);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ConfigModel getConfig() {
return config;
}
}
测试
package com.zking.mymvc.framework;
public class Test {
public static void main(String[] args) {
ConfigModel config=ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentAction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
测试结果:
若我们故意将action中的路径写错
package com.zking.mymvc.framework;
public class Test {
public static void main(String[] args) {
ConfigModel config=ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
效果图如下:就会显示自定义异常该路径找不到
边栏推荐
- Open source SPL eliminates tens of thousands of database intermediate tables
- Typhoon is coming! How to prevent typhoons on construction sites!
- [Yugong series] go teaching course in July 2022 004 go code Notes
- Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
- 清除app data以及获取图标
- Specification of protein quantitative kit for abbkine BCA method
- 解析创客教育的知识迁移和分享精神
- Applet global configuration
- CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
猜你喜欢
Abnova丨荧光染料 620-M 链霉亲和素方案
Open source SPL eliminates tens of thousands of database intermediate tables
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
表单文本框的使用(二) 输入过滤(合成事件)
2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
Duchefa丨D5124 MD5A 培养基中英文说明书
[record of question brushing] 1 Sum of two numbers
Norgen AAV extractant box instructions (including features)
ProSci LAG-3 重组蛋白说明书
随机推荐
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
Applet event binding
MySQL fully parses json/ arrays
资源道具化
Abbkine BCA法 蛋白质定量试剂盒说明书
Fundamentals - configuration file analysis
IC popular science article: those things about Eco
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
Hongmeng OS' fourth learning
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
Popular science | does poor English affect the NPDP exam?
研學旅遊實踐教育的開展助力文旅產業發展
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
Sort and projection
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
小程序全局配置
Point cloud file Dat file read save
Cutting edge technology for cultivating robot education creativity
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
证券开户选择哪个证券比较好?网上开户安全么?