当前位置:网站首页>XML建模
XML建模
2022-06-09 05:34:00 【ˡᵒᵛᵉ一杯美式】
1.什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
2. XML建模
1)根据XML配置文件元素节点创建元素节点实体类
ConfigModel、ActionModel、ForwardModel
2)利用dom4j+xpath技术实现XML建模
ConfigModelFactory
我们为什么要使用XML建模
①XML建模能让我们更加清楚数据结构
② 能让我们更好的在内存中使用数据
我们再来看一下xml结构和要求

从图中得知,一个config里包含多个action元素,action里包含foward元素
首先创建好ConfigModel
package com.zking.xmlmodel.entity;
import java.io.Serializable;
import java.util.HashMap;
/**
* 对应config.xml中config节点所建立的建模实体类
* <config> -> ConfigModel
* 包含关系:ConfigModel -> ActionModel -> ForwardModel(0~N)
* @author Administrator
*
*/
import java.util.Map;
public class ConfigModel implements Serializable {
//key:代表action节点中的name属性:唯一
//value:代表action节点本身
private Map<String, ActionModel> actions = new HashMap<>();
public void push(ActionModel action) {
actions.put(action.getPath(), action);
}
public ActionModel get(String path) {
return actions.get(path);
}
}
这里为什么用Map集合呢?
①其实也可以用list集合,但是因为map集合的key值不能重复,恰好对应action里的path属性,path属性也是不能重复的,之后也方便获取,所以用map集合而不用list集合。
接下来就是config下面的子元素action元素
下面是ActionModel
package com.zking.xmlmodel.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 对应config.xml中action节点所建立的建模实体类
* <action> -> ActionModel
* 包含关系:ActionModel -> ForwardModel(0~N)
* @author Administrator
*
*/
public class ActionModel implements Serializable{
private String path;
private String type;
//key:代表forward节点中的name属性:唯一
//value:代表forward节点本身
private Map<String, ForwardModel> forwards = new HashMap<>();
public void push(ForwardModel forward) {
forwards.put(forward.getName(),forward);
}
public ForwardModel get(String name) {
return forwards.get(name);
}
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 ActionModel() {
super();
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + "]";
}
}
Pattern:这是正则表达式的编译表示。要使用此功能,必须首先在模式类中调用静态方法(编译),该方法返回一个模式对象。
Matcher:这是解释模式并针对输入字符串执行匹配操作的引擎。要获得一个对象,必须在Pattern对象上调用matcher方法。
ActionModel创建好后就创建action元素里的forward
下面是ForwardModel
package com.zking.xmlmodel.entity;
import java.io.Serializable;
/**
* 对应config.xml中forward节点所建立的建模实体类
* <forward> -> ForwardModel
* @author Administrator
*
*/
public class ForwardModel implements Serializable{
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 ForwardModel() {
super();
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
这三个模型建好后就可以开始解析了
下面是我写的ConfigFactory
package com.zking.xmlmodel.util;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.zking.xmlmodel.entity.ActionModel;
import com.zking.xmlmodel.entity.ConfigModel;
import com.zking.xmlmodel.entity.ForwardModel;
public class ConfigModelFactory {
public static final String DEFAULT_PATH="/config.xml";
private ConfigModelFactory() {}
public static ConfigModel createConfigModel() {
return createConfigModel(DEFAULT_PATH);
}
public static ConfigModel createConfigModel(String path) {
ConfigModel configModel = new ConfigModel();
ActionModel actionModel = null;
ForwardModel forwardModel = null;
//目标:使用dom4j+xpath技术实现XML解析建模操作
try {
//1.获取文件输入流
InputStream is =
ConfigModelFactory.class.getResourceAsStream(path);
//2.创建SAXReader对象
SAXReader saxReader = new SAXReader();
//3.读取文件输入流并转换成Document对象
//注:Document包含整个XML中的元素、属性以及文本信息!
Document doc = saxReader.read(is);
//4.准备解析XML
//注意:
//1)获取多个节点:selectNodes
//2)获取单个节点:selectSingleNode
List<Node> actionNodes = doc.selectNodes("config/action");
//5.循环遍历
for (Node action : actionNodes) {
//6.将action节点转换成元素节点(<action>)
Element actionElem = (Element) action;
//7.获取action节点中的所有属性信息(path、type)
String actionPath = actionElem.attributeValue("path");
//8.初始化ActionModel
actionModel = new ActionModel();
actionModel.setPath(actionPath);
actionModel.setType(actionPath);
//9.获取action节点下所有的forward节点(0~N)
List <Node> forwardNodes =
actionElem.selectNodes("forward");
//10.循环遍历forward
for (Node forward : forwardNodes) {
//11.将forward节点转换成元素节点(<forward>)
Element forwardElem = (Element) forward;
//12.获取forward节点的所有属性(name,path,redirect)
String forwardName = forwardElem.attributeValue("name");
String forwardPath = forwardElem.attributeValue("path");
String forwardRedirect = forwardElem.attributeValue("redirect");
//13.初始化forwardElem
forwardModel = new ForwardModel();
forwardModel.setName(forwardName);
forwardModel.setPath(forwardPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
//14.将forwardModel存放到对应的actionModel下
actionModel.push(forwardModel);
}
//15.将actionModel存放到对应的ConfigModel下
configModel.push(actionModel);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return configModel;
}
public static void main(String[] args) {
ConfigModel configModel = ConfigModelFactory.createConfigModel();
//要求:获取config节点下action节点的path属性等于/loginAction的节点对象
ActionModel actionModel = configModel.get("/loginAction");
System.out.println("path="+actionModel.getPath());
System.out.println("type="+actionModel.getType());
//要求:获取action节点下forward节点的name属性等于success
ForwardModel forwardModel = actionModel.get("success");
System.out.println("name="+forwardModel.getName());
System.out.println("path="+forwardModel.getPath());
System.out.println("redirect="+forwardModel.isRedirect());
}
}
以下是执行后的结果

DTD约束:由XML的根节点往里建立约束
XML建模:由最里层节点往根节点进行建模,一个元素节点代表一个实体类
边栏推荐
- Mysql 添加字段或者创建表SQL语句
- TCP error control, flow control, congestion control
- Swift 协议
- lambda匿名函数
- CSV file reading (V3 & V5)
- redis 分布式锁的几种实现方式
- Wamp environment setup (apache+mysql+php)
- Vector of STL
- MQ message loss, message consistency, repeated consumption solution
- A few minutes to understand the Flink waterline
猜你喜欢

1- enter the database

Transaction code qc51 of SAP QM preliminary level creates quality certificate for purchase order

Morsel driven parallelism: a NUMA aware parallel query execution framework

Swagger basic use quick start

Morsel-Driven Parallelism: 一种NUMA感知的并行Query Execution框架

Differences between tinyint and int

Fundamentals of deep learning: face based common expression recognition (2) - data acquisition and collation

reids 缓存与数据库数据不一致、缓存过期删除问题

Recurrence and solution of long jump in data warehouse

Design owlook network novel recommendation system
随机推荐
Heap and priority queues
IP address division and subnet
Swagger basic use quick start
Wechat applet wx Getlocation location error information summary
CSV file reading (V3 & V5)
Leetcode 929.独特的电子邮件地址
STM32 FreeRTOS task Basics
Mining happiness with machine learning
Alibaba cloud AI training camp - SQL basics 3: complex query methods - views, subqueries, functions, etc
Connecting pyqt5 and SQL Server Databases
Leetcode 1037. Effective boomerang
和琪宝的重庆之旅~
YOLOv5的Tricks | 【Trick6】学习率调整策略(One Cycle Policy、余弦退火等)
Several implementation methods of redis distributed lock
pytorch with Automatic Mixed Precision(AMP)
Alibaba cloud AI training camp - SQL basics 4: set operation - addition and subtraction of tables, join, etc
Design owlook network novel recommendation system
Deque of STL
Local redis cluster setup
Data summit 2022 conference information sharing (23 in total)