xml通过dom4j解析成java对象
要解析的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>
java代码解析前一定要先把jar包导入
接下来我来定义实体类
实体类根据上面的xml去定义。实体类定义的顺序一定是由里向外去定义的。
定义顺序 forward标签模型 -> actiong标签模型 -> config标签模型
forward标签模型
public class ForwardModal {
public ForwardModal() {
super();
}
public ForwardModal(String name, String path, boolean redirct) {
super();
this.name = name;
this.path = path;
this.redirct = redirct;
}
private String name;
private String path;
private boolean redirct;
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 isRedirct() {
return redirct;
}
public void setRedirct(boolean redirct) {
this.redirct = redirct;
}
@Override
public String toString() {
return "ForwardModal [name=" + name + ", path=" + path + ", redirct=" + redirct + "]";
}
}
action标签模型
/**
* Action标签模型
* @author 20190313
*
*/
public class ActionModal {
private String path;
private String type;
private Map<String, ForwardModal> map = new HashMap<>();
public void addForwardModal(ForwardModal forwardModal) {
//判断当前的name是不是有重复数据,如果没有则添加,有则跳过
if(!map.containsKey(forwardModal.getName())) {
map.put(forwardModal.getName(), forwardModal);
}
}
public ForwardModal getForwardModal(String key) {
return map.get(key);
}
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;
}
@Override
public String toString() {
return "ActionModal [path=" + path + ", type=" + type + ", map=" + map + "]";
}
}
config标签模型
public class ConfigModal {
private Map<String, ActionModal> map = new HashMap<>();
public void addActionModal(ActionModal actionModal) {
if(!map.containsKey(actionModal.getType())) {
map.put(actionModal.getType(), actionModal);
}
}
public ActionModal getActionModal(String key) {
return map.get(key);
}
@Override
public String toString() {
return "ConfigModal [map=" + map + "]";
}
}
将xml解析成java对象的工厂类
记住解析的方式一定是从外往里。先解析 config节点 config节点里面添加action节点。 action节点中添加forward节点
/**
* 配置文件转成java对象的工厂类
*
* @author 20190313
*
*/
public class ConfigFactory {
private static final String DEFAUL_PATH = "/config.xml";
// 单利模式:只会创建一个对象
private static ConfigModal configModal = null;
/**
* 加载默认路径下的信息
*
* @return
*/
public static ConfigModal createConfig() {
return createConfig(DEFAUL_PATH);
}
/**
* 加载指定目录下的信息
* 返回解析xml后等到的对象(ConfigModal)
* @param path
* @return
*/
public static ConfigModal createConfig(String path) {
if (configModal == null) {
configModal = new ConfigModal();
}
// 开始Dom4j解析
SAXReader saxReader = new SAXReader();
try {
//生成指定路径对应的xml文档对象
Document document = saxReader.read(ConfigFactory.class.getResourceAsStream(path));
// 读取ActionModal对象到configModal中
createAction(document, configModal);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configModal;
}
//这是将action节点解析成ActiongModal对象并放入ConfigModal
private static void createAction(Document document, ConfigModal configModal) {
List actionList = document.selectNodes("/config/action");
//读取当前action节点的属性 并对当前action对象的属性进行赋值
ActionModal actionModal= null;
for (Object object : actionList) {
actionModal = new ActionModal();
Element actionElement = (Element)object;
// 给当前的属性进行赋值
actionModal.setPath(actionElement.attributeValue("path"));
actionModal.setType(actionElement.attributeValue("type"));
//给当前的action添加ForWord的属性
createFrowrd(actionElement, actionModal);
configModal.addActionModal(actionModal);
}
}
//将frowrd节点解析成FrowrdModal并放入ActiongModal对象中
private static void createFrowrd(Element actionElement, ActionModal actionModal) {
List forwardList = actionElement.selectNodes("forward");
ForwardModal forwardModal = null;
for (Object object : forwardList) {
forwardModal = new ForwardModal();
Element forwardElement = (Element)object;
forwardModal.setName(forwardElement.attributeValue("name"));
forwardModal.setPath(forwardElement.attributeValue("path"));
forwardModal.setRedirct(Boolean.valueOf(forwardElement.attributeValue("redirect")));
actionModal.addForwardModal(forwardModal);
}
}
}
测试一下
public static void main(String[] args) {
ConfigModal configModal = ConfigFactory.createConfig();
System.out.println(configModal.getActionModal("test.RegAction").getPath());
System.out.println(configModal.getActionModal("test.RegAction").getType());
System.out.println(configModal.getActionModal("test.RegAction").getForwardModal("failed").getPath());
}
控制台打印结果
/regAction
test.RegAction
/reg.jsp