当前位置:网站首页>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);
}
}
效果图如下:就会显示自定义异常该路径找不到
边栏推荐
- 3.3 project evaluation
- Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
- Duchefa丨低熔点琼脂糖 PPC中英文说明书
- Duchefa丨D5124 MD5A 培养基中英文说明书
- Common view container class components
- 解读协作型机器人的日常应用功能
- E. Singhal and numbers (prime factor decomposition)
- Propping of resources
- Abnova丨荧光染料 620-M 链霉亲和素方案
- Abnova maxpab mouse derived polyclonal antibody solution
猜你喜欢
台风来袭!建筑工地该如何防范台风!
小程序全局配置
Analysis of steam education mode under the integration of five Education
Classic implementation method of Hongmeng system controlling LED
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
Graph embedding learning notes
Duchefa丨MS培养基含维生素说明书
Analyze the knowledge transfer and sharing spirit of maker Education
1、强化学习基础知识点
渗透创客精神文化转化的创客教育
随机推荐
Duchefa cytokinin dihydrozeatin (DHZ) instructions
[Yugong series] go teaching course in July 2022 004 go code Notes
Abnova 环孢素A单克隆抗体,及其研究工具
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
Is the securities account given by the school of Finance and business safe? Can I open an account?
E. Singhal and numbers (prime factor decomposition)
How to renew NPDP? Here comes the operation guide!
Simple understanding of interpolation search
[UE4] unrealinsight obtains the real machine performance test report
Open source SPL eliminates tens of thousands of database intermediate tables
Make Jar, Not War
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
[record of question brushing] 1 Sum of two numbers
Abnova DNA marker high quality control test program
Mongodb basic exercises
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
Monorepo管理方法论和依赖安全
Norgen AAV提取剂盒说明书(含特色)
Abnova丨CRISPR SpCas9 多克隆抗体方案
Duchefa丨D5124 MD5A 培养基中英文说明书