java载入XML文件并解析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
最新文章
- svn中第一次check out working copy项目下来出现 ld: library not found for -lcrypto clang: error: linker command failed with exit code 1 (use -v to see invocation)
- Try to write a script to send e-mail but failed
- SQLite中文排序
- 用一条sql语句显示数据百分比并加百分号
- EntityFramework Core 学习笔记 —— 包含与排除属性
- csharp:Compare two DataTables to rows in one but not the other
- $(document).height()、$(";body";).height()、$(window).height()区别和联系
- matlab mex入门简介
- SAS软件的使用和统计学分析的初步介绍
- Oracle dblink 使用详解
- 深入理解yield(转)
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
- LNMP搭建04 -- 配置Nginx支持PHP
- 微信小程序入门三实战
- BZOJ.4299.Codechef FRBSUM(主席树)
- 洛谷P3295 萌萌哒 并查集 + ST表
- MeshLab显示纹理贴图
- appium+python自动化测试
- python get请求
- C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站
热门文章
- hdu 5637 Transform 最短路
- scrapy中运行爬虫时出现twisted critical unhandled error错误
- CSS自学笔记(3):CSS选择器
- .net mvc下的Areas和小写Url
- Hibernate get 和load的区别
- nfs nobody,nobody 需要在nfs客户端修改从nfs服务器端共享过来的目录怎么办?
- 如何阅读mysql源码
- arduino循迹小车
- 茴香豆的第五种写法---设置ExpandableListView系统自带图标按下效果
- SQL SERVER 2000/2005/2008数据库数据迁移到Oracle 10G细述