码迷,mamicode.com
首页 > 编程语言 > 详细

java加载XML文件并解析xml

时间:2014-10-15 16:59:51      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:java   xml   

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

java加载XML文件并解析xml

标签:java   xml   

原文地址:http://blog.csdn.net/u011096030/article/details/40111321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!