XML建模是指使用XML(可扩展标记语言)对数据进行建模的过程。 在XML建模中,使用XML描述数据结构和数据的关系,这些XML文档可以被解析器解析,从而允许数据被轻松地处理、传递和存储。 XML建模是一种非常灵活的建模方法,它允许您为特定的数据创建自定义结构,以满足您的特定需求。其中最常用的建模方法是通过DTD(文档类型定义)或XML Schema(XML模式)来定义XML文档的结构。在XML建模中,也可以使用关系型数据库,将XML文档转换为关系模型,从而实现数据存储和查询。
重复性的代码较多,对同一个xml文件需要重复的解析,为什么要建模?
以面向对象的思想操作xml文件
一、准备工作:封装
准备工作:
我们新建一个简单的xml文件用来示例:MVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action>
<action path="/loginAction" type="test.LoginAction">
<forward name="failed" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
</config>
新建三个Class类:
ConfigModel、ActionModel、ForardModel
package com.tgq.xmldm;
import java.util.HashMap;
import java.util.Map;
/**
* 根标签对应的对象
*
* @author tgq
*
*/
public class ConfigModel {
private Map<String, ActionModel> maps = new HashMap<String, ActionModel>();
// ActionModel里面两个行为,增加ActionModel,查询ActionModel,所以需要一个map集合
// 编写一个把新的ForwardModel标签加入容器的方法
public void push(ActionModel am) {
maps.put(am.getPath(), am);
}
// 根据ActionModel里面的name值传进去,查到ActionModel
public ActionModel pop(String path) {
return maps.get(path);
}
}
package com.tgq.xmldm;
import java.util.HashMap;
import java.util.Map;
/**
* 对应Action标签
*
* @author tgq
*
*/
public class ActionModel {
// <action path="/loginAction" type="test.LoginAction">
private String path;
private String type;
private Map<String, ForwardModel> maps = 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;
}
// Actionmodel里面两个行为,增加ForwardModel,查询ForwardModel,所以需要一个map集合
// 编写一个把新的ForwardModel标签加入容器的方法
public void push(ForwardModel fm) {
maps.put(fm.getName(), fm);
}
// 根据ForwardModel里面的name值传进去,查到ForwardModel
public ForwardModel pop(String name) {
return maps.get(name);
}
}
package com.tgq.xmldm;
/**
* 对应Forward标签
*
* @author tgq
*
*/
public class ForwardModel {
// <forward name="success" path="/login.jsp" redirect="true" />
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;
}
}
二、建模
1、普通
我们新建一个Demo的class文件进行测试:
package com.tgq.xmldm;
/**
* 测试
*
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
ConfigModel cm = new ConfigModel();
ActionModel am = cm.pop("/loginAction");
System.out.println(am.getType());
}
}
他会报错,是拿不到值的,因为我们没拿到xml里面的属性的值,所以我们要去建立一个23种设计模式中的一种工厂模式 。
新建一个工厂类:ConfigModelFactory 编写一个获取xml属性值的方法
代码如下:
public static ConfigModel bulid() throws Exception {
String path = "/MVC.xml";
// 拿到xml文件
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
// 实例化读取类
SAXReader sr = new SAXReader();
// 读写xml文件
Document read = sr.read(is);
// 拿到xml文件下的action标签属性
List<Element> list = read.selectNodes("/config/action");
// 实例化ConfigModel,后面进行添加
ConfigModel cm = new ConfigModel();
// 循环拿到action里面的属性值
for (Element element : list) {
// 实例化ActionModel
ActionModel am = new ActionModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
am.setPath(element.attributeValue("path"));
am.setType(element.attributeValue("type"));
// 打印测试是否能拿到值
System.out.println(element.attributeValue("path"));
cm.push(am);
}
return cm;
}
输出结果:
/regAction
failed
success
这是拿到action标签里面属性的值,我们再进行一个优化
方法代码如下:
public static ConfigModel bulid() throws Exception {
// 拿到xml文件
InputStream is = ConfigModelFactory.class.getResourceAsStream("MVC.xml");
// 实例化读取类
SAXReader sr = new SAXReader();
// 读写xml文件
Document read = sr.read(is);
// 拿到xml文件下的action标签属性
List<Element> list = read.selectNodes("/config/action");
// 实例化ConfigModel,后面进行添加
ConfigModel cm = new ConfigModel();
// 循环拿到action里面的属性值
for (Element element : list) {
// 实例化ActionModel
ActionModel am = new ActionModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
am.setPath(element.attributeValue("path"));
am.setType(element.attributeValue("type"));
// 打印测试是否能拿到值
System.out.println(element.attributeValue("path"));
// 拿到element,再拿到forward
List<Element> actionlist = element.selectNodes("forward");
for (Element element2 : actionlist) {
// 实例化ForwardModel
ForwardModel fm = new ForwardModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
fm.setName(element2.attributeValue("name"));
fm.setPath(element2.attributeValue("path"));
// 因为forward里面的属性Redirect值在ForwardModel是boolean类型,所以要进行一个判断
fm.setRedirect("true".equals(element2.attributeValue("redirect")));
// 打印测试是否能拿到值
System.out.println("=================================");
System.out.println(element2.attributeValue("name"));
am.push(fm);
}
cm.push(am);
}
return cm;
}
输出结果:
/regAction
failed
success
=========================
/loginAction
failed
success
2、优化
为了更好的方便使用,我们会编写两个bulid()方法,一个有参一个默认无参。
代码如下:
public static ConfigModel bulid(String path) throws Exception {
// 拿到xml文件
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
// 实例化读取类
SAXReader sr = new SAXReader();
// 读写xml文件
Document read = sr.read(is);
// 拿到xml文件下的action标签属性
List<Element> list = read.selectNodes("/config/action");
// 实例化ConfigModel,后面进行添加
ConfigModel cm = new ConfigModel();
// 循环拿到action里面的属性值
for (Element element : list) {
// 实例化ActionModel
ActionModel am = new ActionModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
am.setPath(element.attributeValue("path"));
am.setType(element.attributeValue("type"));
// 打印测试是否能拿到值
System.out.println(element.attributeValue("path"));
// 拿到element,再拿到forward
List<Element> actionlist = element.selectNodes("forward");
for (Element element2 : actionlist) {
// 实例化ForwardModel
ForwardModel fm = new ForwardModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
fm.setName(element2.attributeValue("name"));
fm.setPath(element2.attributeValue("path"));
// 因为forward里面的属性Redirect值在ForwardModel是boolean类型,所以要进行一个判断
fm.setRedirect("true".equals(element2.attributeValue("redirect")));
// 打印测试是否能拿到值
System.out.println(element2.attributeValue("name"));
am.push(fm);
}
cm.push(am);
}
return cm;
}
public static ConfigModel bulid() throws Exception {
String path = "/MVC.xml";
return bulid(path);
}
3、测试
最后我们再demo里面测试一下是否有获取到值
package com.tgq.xmldm;
public class Demo1 {
public static void main(String[] args) throws Exception {
ConfigModel cm = ConfigModelFactory.bulid();
ActionModel am = cm.pop("/loginAction");
System.out.println(am.getType());
}
}
输出结果:
test.LoginAction
我们测试一下是否拿到forward标签里面的success属性值
package com.tgq.xmldm;
public class Demo1 {
public static void main(String[] args) throws Exception {
ConfigModel cm = ConfigModelFactory.bulid();
ActionModel am = cm.pop("/loginAction");
System.out.println(am.getType());
ForwardModel fm=am.pop("success");
System.out.println(fm.getPath());
}
}
测试结果:
test.LoginAction
/main.jsp
4、完整ConfigModelFactory 代码
package com.tgq.xmldm;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 23种设计模式---->工厂模式
*
* ConfigModelFactory用来生产ConfigModel对象的 生成的ConfigModel对象包含了Config.xml中的配置内容
*
* @author tgq
*
*/
public class ConfigModelFactory {
@SuppressWarnings("unchecked")
public static ConfigModel bulid(String path) throws Exception {
// 拿到xml文件
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
// 实例化读取类
SAXReader sr = new SAXReader();
// 读写xml文件
Document read = sr.read(is);
// 拿到xml文件下的action标签属性
List<Element> list = read.selectNodes("/config/action");
// 实例化ConfigModel,后面进行添加
ConfigModel cm = new ConfigModel();
// 循环拿到action里面的属性值
for (Element element : list) {
// 实例化ActionModel
ActionModel am = new ActionModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
am.setPath(element.attributeValue("path"));
am.setType(element.attributeValue("type"));
// 打印测试是否能拿到值
// System.out.println(element.attributeValue("path"));
// 拿到element,再拿到forward
List<Element> actionlist = element.selectNodes("forward");
for (Element element2 : actionlist) {
// 实例化ForwardModel
ForwardModel fm = new ForwardModel();
// 拿到xml文件里面的属性值并放在对应的Model里面
fm.setName(element2.attributeValue("name"));
fm.setPath(element2.attributeValue("path"));
// 因为forward里面的属性Redirect值在ForwardModel是boolean类型,所以要进行一个判断
fm.setRedirect("true".equals(element2.attributeValue("redirect")));
// 打印测试是否能拿到值
// System.out.println(element2.attributeValue("name"));
am.push(fm);
}
cm.push(am);
}
return cm;
}
public static ConfigModel bulid() throws Exception {
String path = "/MVC.xml";
return bulid(path);
}
public static void main(String[] args) throws Exception {
bulid();
}
}
希望对你们有用!!!