【XML】建模

建模的简介与思路

建模的由来:就是将指定的xml字符串当作对象来操作

建模的优点:如果说当对一个指定的xml格式字符串完成了建模操作,只需要调用指定的方法就可以完成预定的字符串获取。

建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象

好处:提高代码的复用性

XML建模
ConfigModel类

package com.zking.xml.mode;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	private Map<String, ActionModel> acMap = new HashMap<>();
	public void push(ActionModel actionModel) {
		acMap.put(actionModel.getPath(), actionModel);
	}
	public ActionModel pop(String path) {
		return acMap.get(path);
	}
	public static void main(String[] args) {
		ConfigModel configModel  = new ConfigModel();	//null a null
		ActionModel a = configModel.pop("/LoginAction");
	}
}

ActionModel类

package com.zking.xml.mode;

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
//	<action path="/LoginAction" type="test.action.LoginAction">
	private String path;
	private String type;
	private Map<String, ForwardModel> fMap = new HashMap<>();
	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 void pash(ForwardModel forwarModel) {
		fMap.put(forwarModel.getName(), forwarModel);
	}
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
}

ForwardModel类

package com.zking.xml.mode;

public class ForwardModel {
//	<forward name="a" path="/index.jsp" redirect="false" />
	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;
	}
}

ConfigModelFactory类

package com.zking.xml.mode;

import java.io.InputStream;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigModelFatory {
	public static ConfigModel build() throws Exception {
		return build("config.xml");
	}

	private static ConfigModel build(String xmlPath) throws Exception {
		ConfigModel configModel = new ConfigModel();
		InputStream in = ConfigModelFatory.class.getResourceAsStream(xmlPath);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(in);
		ActionModel actionModel  = null;
		ForwardModel forwardModel = null;
		List<Element> actionEles = doc.selectNodes("/config/action");
		for (Element actionEle : actionEles) {
			actionModel = new ActionModel();
			//接下来需要往actionModel中填充内容
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
			List<Element> forwardEles = actionEle.selectNodes("forward");
			for (Element  forwardEle: forwardEles) {
				forwardModel = new ForwardModel();
				//接下来需要往forwardModel中填充内容
				forwardModel.setName(forwardEle.attributeValue("name"));
				forwardModel.setPath(forwardEle.attributeValue("path"));
				forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
				actionModel.pash(forwardModel);
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
	public static void main(String[] args) throws Exception {
		ConfigModel configModel = ConfigModelFatory.build();
		ActionModel actionModel = configModel.pop("/LoginAction");
		System.out.println(actionModel.getType());
		ForwardModel forwardModel  = actionModel.pop("b");
		System.out.println(forwardModel.getName() + "  " + forwardModel.isRedirect());
	}
}

输出后
在这里插入图片描述
:属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值
建模分两步
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。

想要了解更多,私信博主。

猜你喜欢

转载自blog.csdn.net/zyp_baoku/article/details/90611099
xml