使用Dom4j解析XML

来源:互联网 发布:mysql第一第二第三范式 编辑:程序博客网 时间:2024/06/06 19:30

dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.

       对主流的Java XML API进行的性能、功能和易用性的评测,dom4j无论在那个方面都是非常出色的。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,例如Hibernate,包括sun公司自己的JAXM也用了Dom4j。

下面用dom4j来解析mystruts.xml

xml代码

<?xml version="1.0" encoding="UTF-8"?><mystruts><package><action name="login" class="cn.albin.framework.action.LoginAction" method="login"><result name="loginSuccess" type="redirect">/index.jsp</result><result name="loginFaild">/login.jsp</result></action><action name="register" class="cn.albin.framework.action.RegisterAction" method="register"><result name="registerSuccess">/login</result></action></package></mystruts>
Result实体类

/** * 封装结果视图 * <result name="success" type="redirect">/index.jsp</result> * */public class Result {// 跳转的结果标记private String name;// 跳转类型,默认为转发; "redirect"为重定向private String type;// 跳转的页面private String page;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getPage() {return page;}public void setPage(String page) {this.page = page;}}

ActionMapping实体类

import java.util.Map;/** * 封装action节点 *      <action name="login" class="cn.albin.framework.action.LoginAction" method="login"><result name="success" type="redirect">/index.jsp</result><result name="loginFaild">/login.jsp</result></action> * */public class ActionMapping {// 请求路径名称private String name;// 处理aciton类的全名private String className;// 处理方法private String method;// 结果视图集合private Map<String,Result> results;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}public Map<String, Result> getResults() {return results;}public void setResults(Map<String, Result> results) {this.results = results;}}

ActionMappingManager实体类

import java.io.InputStream;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;/** * 加载配置文件, 封装所有的整个mystruts.xml * * */public class ActionMappingManager {// 保存action的集合private Map<String,ActionMapping> allActions ;public ActionMappingManager(){allActions = new HashMap<String,ActionMapping>();// 初始化this.init();}/** * 根据请求路径名称,返回Action的映射对象 * @param actionName   当前请求路径 * @return             返回配置文件中代表action节点的AcitonMapping对象 */public ActionMapping getActionMapping(String actionName) {if (actionName == null) {throw new RuntimeException("传入参数有误,请查看struts.xml配置的路径。");}ActionMapping actionMapping = allActions.get(actionName);if (actionMapping == null) {throw new RuntimeException("路径在struts.xml中找不到,请检查");}return actionMapping;}// 初始化allActions集合private void init() {/********DOM4J读取配置文件***********/try {// 1. 得到解析器SAXReader reader = new SAXReader();// 得到src/mystruts.xml  文件流InputStream inStream = this.getClass().getResourceAsStream("/mystruts.xml");// 2. 加载文件Document doc = reader.read(inStream);// 3. 获取根Element root = doc.getRootElement();// 4. 得到package节点Element ele_package = root.element("package");// 5. 得到package节点下,  所有的action子节点List<Element> listAction = ele_package.elements("action");// 6.遍历 ,封装for (Element ele_action : listAction) {// 6.1 封装一个ActionMapping对象ActionMapping actionMapping = new ActionMapping();actionMapping.setName(ele_action.attributeValue("name"));actionMapping.setClassName(ele_action.attributeValue("class"));actionMapping.setMethod(ele_action.attributeValue("method"));// 6.2 封装当前aciton节点下所有的结果视图Map<String,Result> results = new HashMap<String, Result>();// 得到当前action节点下所有的result子节点 Iterator<Element> it = ele_action.elementIterator("result"); while (it.hasNext()) { // 当前迭代的每一个元素都是 <result...> Element ele_result = it.next();  // 封装对象 Result res = new Result(); res.setName(ele_result.attributeValue("name")); res.setType(ele_result.attributeValue("type")); res.setPage(ele_result.getTextTrim());  // 添加到集合 results.put(res.getName(), res); }// 设置到actionMapping中actionMapping.setResults(results);// 6.x actionMapping添加到map集合allActions.put(actionMapping.getName(), actionMapping);}} catch (Exception e) {throw new RuntimeException("启动时候初始化错误",e);}}}



0 0
原创粉丝点击