一、什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二、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标签:可以包含0~N个action标签
-->
<config>
<!--
action标签:可以饱含0~N个forward标签
path:以/开头的字符串,并且值必须唯一 非空
type:字符串,非空
-->
<action path="/regAction" type="test.RegAction">
<!--
forward标签:没有子标签;
name:字符串,同一action标签下的forward标签name值不能相同 ;
path:以/开头的字符串
redirect:只能是false|true,允许空,默认值为false
-->
<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>
ForwardModul
package com.zking.xml.entity;
import java.io.Serializable;
public class ForwardModul implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String path;
private Boolean redirect;
public ForwardModul() {
}
public String getNmae() {
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 getRedirect() {
return redirect;
}
public void setRedirect(Boolean redirect) {
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardModul [name=" + name + ", type=" + path + ", redirect=" + redirect + "]";
}
}
ActionModul
package com.zking.xml.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ActionModul implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String path;
private String type;
private Map<String,ForwardModul> ActionMpdul = new
HashMap<String, ForwardModul>();
/**
* 增加ForwardModul对象
* @param forwardModul
*/
public void push(ForwardModul forwardModul) {
this.ActionMpdul.put(forwardModul.getNmae(),forwardModul);
}
/**
* 查找对应的对象
* @param name 根据键拿到值
* @return
*/
public ForwardModul getForword(String name) {
return this.ActionMpdul.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 Map<String, ForwardModul> getActionMpdul() {
return ActionMpdul;
}
public void setActionMpdul(Map<String, ForwardModul> actionMpdul) {
ActionMpdul = actionMpdul;
}
public ActionModul() {
// TODO Auto-generated constructor stub
}
}
ConfigModul
package com.zking.xml.entity;
import java.io.Serializable;
import java.util.HashMap;
/**
* 建模实体类 config
* @author zjjt
*
*/
import java.util.Map;
public class ConfigModul implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Map<String,ActionModul> ConfigModul =
new HashMap<String, ActionModul>();
public Map<String, ActionModul> getConfigModul() {
return ConfigModul;
}
public void setConfigModul(Map<String, ActionModul> configModul) {
ConfigModul = configModul;
}
public ConfigModul() {
// TODO Auto-generated constructor stub
}
/**
* 将actionModul对象存到Config集合中
* @param Actionmodul
*/
public void push(ActionModul Actionmodul) {
this.ConfigModul.put(Actionmodul.getPath(),Actionmodul);
}
/**
* 查找 根据键拿到值
* @param path
* @return
*/
public ActionModul getActionMOdul(String path) {
return this.ConfigModul.get(path);
}
}
工具类
package com.zking.xml.util;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.zking.xml.entity.ActionModul;
import com.zking.xml.entity.ConfigModul;
import com.zking.xml.entity.ForwardModul;
public class JianMo {
private static final String PATH = "/config.xml";
private JianMo() {
}
public static ConfigModul getModel(String path) {
//获取io流
InputStream is = JianMo.class.getResourceAsStream(path);
//实例化解析xml工具类 SAXReader
SAXReader srd = new SAXReader();
//实例化
String ActionPath = null;
String Actiontype = null;
ActionModul Actionmodul = null;
String ForwardName = null;
String ForwardPath = null;
String Forwardredirect = null;
ForwardModul forwardmodel = null;
ConfigModul configModul = new ConfigModul();
try {
//读取xml文件 得到document对象
Document document = srd.read(is);
//找到config节点下的anction
List<Node> ActionNodes = document.selectNodes("/config/action");
//System.out.println(ActionNodes.size());
for (Node node : ActionNodes) {
//将文本对象转为元素对象
Element el = (Element)node;
//拿属性值根据属性名拿对的属性值
ActionPath = el.attributeValue("path");
Actiontype = el.attributeValue("type");
Actionmodul = new ActionModul();
Actionmodul.setPath(ActionPath);
Actionmodul.setType(Actiontype);
//获取action节点下的forword节点
List<Node> forwardNodes = node.selectNodes("forward");
for (Node node2 : forwardNodes) {
//将文本对象转为元素对象
Element forwardnel = (Element)node2;
//拿属性值根据属性名拿对的属性值
ForwardName = forwardnel.attributeValue("name");
ForwardPath = forwardnel.attributeValue("path");
forwardmodel = new ForwardModul();
Forwardredirect = forwardnel.attributeValue("redirect");
forwardmodel.setName(ForwardName);
forwardmodel.setPath(ForwardPath);
forwardmodel.setRedirect(Boolean.parseBoolean(Forwardredirect));
Actionmodul.push(forwardmodel);
}
configModul.push(Actionmodul);
}
} catch (Exception e) {
e.printStackTrace();
}
return configModul;
}
public static ConfigModul getModel() {
return getModel(PATH);
}
public static void main(String[] args) {
ConfigModul model2 = getModel();
//根据键拿到ActionModul对象 这个值也就是xml里面所对应的path值
ActionModul actionMOdul = model2.getActionMOdul("/regAction");
//根据键拿到ForwordModul对象 这个值也就是xml里面所对应的name值
ForwardModul forword = actionMOdul.getForword("failed");
System.out.println(actionMOdul.getType());
System.out.println(forword.getNmae()+""+forword.getPath());
}
}
运行效果图: