日期:2014-05-20  浏览次数:20810 次

求高手帮忙关于解析XML文件获得属性问题
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "applicationActions.dtd">
<beans>
<!--登录-->
<action>
<name>login</name>
<value>com.cxt.hr.model.LoginAction</value>
<success type="redirect">welcome.jsp</success>
<fail type="forward">login.jsp</fail>
<error>error.jsp</error>
</action>
<!--查找员工-->
<action>
<name>findEmployee</name>
<value>com.cxt.hr.model.FindEmployeeAction</value>
<success type="forward">listEmployees.jsp</success>
<fail type="forward">findEmployee.jsp</fail>
<error>error.jsp</error>
</action>
下面是我获得节点的方法,我不知道怎么获得success和fail参数属性,用的DOM4J解析,求教下具体操作
// 开始解析,返回文档对象
Document doc = reader.read(XmlParse.class
.getResource("/applicationActions.xml"));
Element root = doc.getRootElement();
List<?> list = root.elements("action");// 返回子结点的集合
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Element element = (Element) it.next();
Action action = new Action();
String name = element.elementText("name");// action的名称
String value = element.elementText("value");// action的路径
String success = element.elementText("success");// 成功返回的页面
String fail = element.elementText("fail");// 失败返回的页面
String error = element.elementText("error");// 出错返回的页面
action.setName(name);
action.setValue(value);
action.setSuccess(success);
action.setFail(fail);
action.setError(error);
map.put(action.getName(), action);
}
} catch (DocumentException e) {
logger.error(e.getMessage());
throw new RuntimeException(e);
}
return map;

------解决方案--------------------
Java code
String success = element.elementText("success");// 成功返回的页面
            /*取succes属性值*/
            Element node=element.element("success");
            Iterator type=node.attributeIterator();
            while(type.hasNext()){
                Attribute objAttr= (Attribute)type.next();
                System.out.println(objAttr.getName()+"="+objAttr.getValue());
                
            }