Dom4j 解析XML文件
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<action name="login" class="com.coderising.action.LoginAction">
<result name="success">/jsp/homepage.jsp
</result>
<result name="fail">/jsp/showLogin.jsp
</result>
</action>
<action name="logout" class="com.coderising.action.LogoutAction">
<result name= "success">/jsp/welcome.jsp
</result>
<result name= "fail">/jsp/error.jsp
</result>
</action>
</struts>
一、DOM4j中,获得Document对象的方式
遍历节点
得到根节点之后,通过遍历根节点得到具体某个节点内容 也可以通过指定节点名称的方式获取摸个节点
List<Element> elements = root.elements();
for (Element element : elements) {
Attribute actionAttribute = element.
attribute(
"name");
Attribute classAttribute = element.
attribute(
"class");
}
通过attribute得到节点的具体内容
String name = entry.getKey();
String value = entry.getValue();
通过attribute得到节点的子节点内容
List<Element> element1s = element
.elements(
"result")
if (result
.equals(
"success")) {
for (int i =
0
Attribute attribute2 =element1s
.get(i)
.attribute(
"name")
if (attribute2
.getValue()
.equals(
"success")) {
//得到result标签下的text内容
jspPath = element1s
.get(i)
.getStringValue()
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-2753.html