xml解析java对象_xml通过dom4j解析成java对象

要解析的xml

java代码解析前一定要先把jar包导入

95de340700e8f0a08dad056aa3ad86b3.png

接下来我来定义实体类

实体类根据上面的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 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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值