import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File("d://demo.xml"));
Element root = document.getRootElement();
List actions = root.elements("action");
for (int i = 0; i < actions.size(); i++) {
Element action = (Element) actions.get(i);
System.out.println("\n" + "action.path--->" + action.attributeValue("path"));
System.out.println("action.class--->" + action.attributeValue("class"));
List forwards = action.elements("forward");
for(int j=0;j<forwards.size();j++){
Element forward = (Element) forwards.get(j);
System.out.println("forward.name--->" + forward.attributeValue("name"));
System.out.println("forward.url--->" + forward.attributeValue("url"));
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<!--xml文件-->
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action path="/chen" class="com.demo.testCHEN">
<forward name="success" url="xiao-chen"/>
<forward name="fail" url="da-chen"/>
</action>
<action path="/bei" class="com.demo.testBEI">
<forward name="success" url="xiao-bei"/>
<forward name="fail" url="da-bei"/>
</action>
</actions>
运行结果:
action.path--->/chen
action.class--->com.demo.testCHEN
forward.name--->success
forward.url--->xiao-chen
forward.name--->fail
forward.url--->da-chen
action.path--->/bei
action.class--->com.demo.testBEI
forward.name--->success
forward.url--->xiao-bei
forward.name--->fail
forward.url--->da-bei