java解析xml文件(dom4j方式)
java解析xml文件的方式有几种,下面介绍用dom4j方式解析
1、先看一段xml内容
2、看java代码:
先获取configs里面的所有config,然后对它进行迭代,获取每一个config。
接下来是获取config里面的节点了,如果config里的子节点没有“后代”了,如mode节点,那么直接String modeValue = config.element("mode").getText();就获取值了,
但是如果config里的有些子节点还有子节点,如files节点,就List<Element> files = config.element("files").elements();放到一个集合里,然后循环取出。
1、先看一段xml内容
1
<
configs
>
2
3 < config key = " 01002 " name = " 支付申请表 " order = " 1 " > <!-- key为config键。 order为级别 ,1为最高级别,级别越高,放在最前面,相同级别无须 -->
4 <!-- 取数模式 D:按日生成 M:按月生成 Y:按年生成 -->
5 < mode > D </ mode >
6 <!-- 依赖数据,需校验parent是否已生成成功 -->
7 < parents ></ parents >
8 <!-- 初始抽取时间 -->
9 < defaultTime > 2013 - 05 - 01 </ defaultTime >
10 <!-- 数据源SQL -->
11 < sqls >
12 < sql >
13 <! [CDATA[
14 select * from XXX
15 ]] >
16 </ sql >
17 < sql >
18 <! [CDATA[
19 select * from XXX
20 ]] >
21 </ sql >
22 </ sqls >
23 <!-- 上传数据源字段类型及长度 -->
24 < colTypes >
25 < colType >
26 < type > number </ type >
27 < length > 18 </ length >
28 </ colType >
29 < colType >
30 < type > string </ type >
31 < length > 10 </ length ><!-- char或者varchar统一定义为string -->
32 </ colType >
33 </ colTypes >
34 <!-- 上传、下载文件生成配置 ,最多4个文件,数据文件、校验文件、文件级校验报告、记录级校验报告 -->
35 < files >
36 < file type = " data " >
37 < name > a_10100_EBS_{YYYYMMDD - D}_01002_{NO}_{SERIAL} </ name > <!-- 文件名, {}扩起部分为通配,YYYYMMDD - D代表数据日期,YYYYMMDD - S代表系统日期 -->
38 < suffix > .dat </ suffix > <!-- 生成的文件后缀名 -->
39 < code > GBK </ code > <!-- 生成的文件编码格式 -->
40 < compress > GZ </ compress > <!-- 生成文件需压缩的文件类型,自定义通配 -->
41 < size > 2000000000 </ size > <!-- 文件大小,超出则分割, byte -->
42 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
43 < path > data </ path > <!-- 上传、下载的路径 -->
44 </ file >
45 < file type = " check " >
46 < name > a_10100_EBS_{YYYYMMDD - D}_01002_{NO} </ name >
47 < suffix > .verf </ suffix >
48 < code > GBK </ code >
49 < compress > GZ </ compress >
50 < size > 1024 </ size >
51 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
52 < path > data </ path >
53 </ file >
54 </ files >
55 </ config >
56
57 < config key = " 01004 " name = " 调拨申请表 " order = " 1 " >
58 <!-- 取数模式 D:按日生成 M:按月生成 Y:按年生成 -->
59 < mode > D </ mode >
60 <!-- 依赖数据,需校验parent是否已生成成功 -->
61 < parents ></ parents >
62 <!-- 初始抽取时间 -->
63 < defaultTime > 2013 - 05 - 01 </ defaultTime >
64 <!-- 数据源SQL -->
65 < sqls >
66 < sql >
67 <! [CDATA[
68 select * from XXX
69 ]] >
70 </ sql >
71 < sql >
72 <! [CDATA[
73 select * from XXX
74 ]] >
75 </ sql >
76 </ sqls >
77 <!-- 上传数据源字段类型及长度 -->
78 < colTypes >
79 < colType >
80 < type > number </ type >
81 < length > 18 </ length >
82 </ colType >
83 < colType >
84 < type > string </ type >
85 < length > 10 </ length >
86 </ colType >
87 </ colTypes >
88 <!-- 上传文件生成配置 ,最多2个文件,数据文件和校验文件,不需要校验文件,可屏蔽 -->
89 < files >
90 < file type = " data " >
91 < name > a_10100_EBS_{YYYYMMDD - D}_01004_{NO}_{SERIAL} </ name > <!-- 文件名, {}扩起部分为通配 ,YYYYMMDD - D代表数据日期,YYYYMMDD - S代表系统日期 -->
92 < suffix > .dat </ suffix > <!-- 生成的文件后缀名 -->
93 < code > GBK </ code > <!-- 生成的文件编码格式 -->
94 < compress > GZ </ compress > <!-- 生成文件需压缩的文件类型,自定义通配 -->
95 < size > 2000000000 </ size > <!-- 文件大小,超出则分割, byte -->
96 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
97 < path > data </ path >
98 </ file >
99 < file type = " check " >
100 < name > a_10100_EBS_{YYYYMMDD - D}_01004_{NO} </ name >
101 < suffix > .verf </ suffix >
102 < code > GBK </ code >
103 < compress > GZ </ compress >
104 < size > 1024 </ size >
105 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
106 < path > data </ path >
107 </ file >
108 </ files >
109 </ config >
110
111 </ configs >
别管xml要提供的功能,只看结构,根节点是configs,一级节点是两个config,然后config里面有多个子节点mode、parents、defaultTime、sqls、colTypes、file,然后sqls等等还有子节点,那么要怎么一级一级获取呢?
2
3 < config key = " 01002 " name = " 支付申请表 " order = " 1 " > <!-- key为config键。 order为级别 ,1为最高级别,级别越高,放在最前面,相同级别无须 -->
4 <!-- 取数模式 D:按日生成 M:按月生成 Y:按年生成 -->
5 < mode > D </ mode >
6 <!-- 依赖数据,需校验parent是否已生成成功 -->
7 < parents ></ parents >
8 <!-- 初始抽取时间 -->
9 < defaultTime > 2013 - 05 - 01 </ defaultTime >
10 <!-- 数据源SQL -->
11 < sqls >
12 < sql >
13 <! [CDATA[
14 select * from XXX
15 ]] >
16 </ sql >
17 < sql >
18 <! [CDATA[
19 select * from XXX
20 ]] >
21 </ sql >
22 </ sqls >
23 <!-- 上传数据源字段类型及长度 -->
24 < colTypes >
25 < colType >
26 < type > number </ type >
27 < length > 18 </ length >
28 </ colType >
29 < colType >
30 < type > string </ type >
31 < length > 10 </ length ><!-- char或者varchar统一定义为string -->
32 </ colType >
33 </ colTypes >
34 <!-- 上传、下载文件生成配置 ,最多4个文件,数据文件、校验文件、文件级校验报告、记录级校验报告 -->
35 < files >
36 < file type = " data " >
37 < name > a_10100_EBS_{YYYYMMDD - D}_01002_{NO}_{SERIAL} </ name > <!-- 文件名, {}扩起部分为通配,YYYYMMDD - D代表数据日期,YYYYMMDD - S代表系统日期 -->
38 < suffix > .dat </ suffix > <!-- 生成的文件后缀名 -->
39 < code > GBK </ code > <!-- 生成的文件编码格式 -->
40 < compress > GZ </ compress > <!-- 生成文件需压缩的文件类型,自定义通配 -->
41 < size > 2000000000 </ size > <!-- 文件大小,超出则分割, byte -->
42 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
43 < path > data </ path > <!-- 上传、下载的路径 -->
44 </ file >
45 < file type = " check " >
46 < name > a_10100_EBS_{YYYYMMDD - D}_01002_{NO} </ name >
47 < suffix > .verf </ suffix >
48 < code > GBK </ code >
49 < compress > GZ </ compress >
50 < size > 1024 </ size >
51 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
52 < path > data </ path >
53 </ file >
54 </ files >
55 </ config >
56
57 < config key = " 01004 " name = " 调拨申请表 " order = " 1 " >
58 <!-- 取数模式 D:按日生成 M:按月生成 Y:按年生成 -->
59 < mode > D </ mode >
60 <!-- 依赖数据,需校验parent是否已生成成功 -->
61 < parents ></ parents >
62 <!-- 初始抽取时间 -->
63 < defaultTime > 2013 - 05 - 01 </ defaultTime >
64 <!-- 数据源SQL -->
65 < sqls >
66 < sql >
67 <! [CDATA[
68 select * from XXX
69 ]] >
70 </ sql >
71 < sql >
72 <! [CDATA[
73 select * from XXX
74 ]] >
75 </ sql >
76 </ sqls >
77 <!-- 上传数据源字段类型及长度 -->
78 < colTypes >
79 < colType >
80 < type > number </ type >
81 < length > 18 </ length >
82 </ colType >
83 < colType >
84 < type > string </ type >
85 < length > 10 </ length >
86 </ colType >
87 </ colTypes >
88 <!-- 上传文件生成配置 ,最多2个文件,数据文件和校验文件,不需要校验文件,可屏蔽 -->
89 < files >
90 < file type = " data " >
91 < name > a_10100_EBS_{YYYYMMDD - D}_01004_{NO}_{SERIAL} </ name > <!-- 文件名, {}扩起部分为通配 ,YYYYMMDD - D代表数据日期,YYYYMMDD - S代表系统日期 -->
92 < suffix > .dat </ suffix > <!-- 生成的文件后缀名 -->
93 < code > GBK </ code > <!-- 生成的文件编码格式 -->
94 < compress > GZ </ compress > <!-- 生成文件需压缩的文件类型,自定义通配 -->
95 < size > 2000000000 </ size > <!-- 文件大小,超出则分割, byte -->
96 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
97 < path > data </ path >
98 </ file >
99 < file type = " check " >
100 < name > a_10100_EBS_{YYYYMMDD - D}_01004_{NO} </ name >
101 < suffix > .verf </ suffix >
102 < code > GBK </ code >
103 < compress > GZ </ compress >
104 < size > 1024 </ size >
105 < tempPath > temp </ tempPath ><!-- 临时存放路径 -->
106 < path > data </ path >
107 </ file >
108 </ files >
109 </ config >
110
111 </ configs >
2、看java代码:
1
try
{
2 String path = InterfaceConfig. class .getResource( "" ).getPath().toString(); // 获取类所在路径
3 if (path.contains( " .jar " )) {
4 path = path.replace( " / " , File.separator); // 将/换成\
5 path = path.replace( " file: " , "" ); // 去掉file
6 path = path.replace( " classes\\ " , "" ); // 去掉classes\
7 if (path.startsWith( " \\ " )) {
8 path = path.substring( 1 ); // 去掉第一个\,如:、\D:\TongWeb
,在linux上没有这种情况
9 }
10 path = path.split( " WEB-INF " )[ 0 ] + " WEB-INF " + File.separator + " classes " ;
11 } else {
12 path = InterfaceConfig. class .getResource( " / " ).getPath().toString(); // 获取根路径
13 }
14 // String realPath = ActionUtils.getRequest().getRealPath("/")+File.separator+"WEB-INF"+File.separator+"classes";
15 File file = new File(path + File.separator + " InterfaceConfig.xml " );
16 SAXReader reader = new SAXReader();
17 Document doc = reader.read(file);
18 Element root = doc.getRootElement();
19 Iterator < Element > configs = root.elementIterator( " config " );
20 InterfaceConfig ifc = null ;
21 while (configs.hasNext()) {
22 ifc = new InterfaceConfig();
23 Element config = configs.next();
24
25 String key = config.attributeValue( " key " );
26 String name = config.attributeValue( " name " );
27 String order = config.attributeValue( " order " );
28 ifc.setKey(key);
29 ifc.setName(name);
30 ifc.setOrder(order);
31
32 // 获取config下的mode
33 String modeValue = config.element( " mode " ).getText();
34 ifc.setMode(modeValue);
35
36 // 获取初始抽取时间
37 String defaultTime = config.element( " defaultTime " ).getText();
38 ifc.setDefaultTime(defaultTime);
39
40 // 获取config下的parents
41 List < Element > parents = config.element( " parents " ).elements();
42 List < String > parentsList = new ArrayList < String > ();
43 for (Element e : parents) {
44 parentsList.add(e.getText());
45 }
46 ifc.setParents(parentsList);
47
48 // 获取config下的sqls
49 List < Element > sqls = config.element( " sqls " ).elements();
50 List < String > sqlsList = new ArrayList < String > ();
51 for (Element e : sqls) {
52 String sqlValue = e.getText().replaceAll( " \n " , " " ).replaceAll( " \t " , " " );
53 sqlsList.add(sqlValue);
54 }
55 ifc.setSqls(sqlsList);
56
57 // 获取config下的colTypes
58 List < Element > colTypes = config.element( " colTypes " ).elements();
59 List < ColType > ctList = new ArrayList < ColType > ();
60 ColType ct = null ;
61 for (Element e : colTypes) {
62 ct = new ColType();
63 ct.setType(e.element( " type " ).getText());
64 ct.setLength(e.element( " length " ).getText());
65 ctList.add(ct);
66 }
67 ifc.setColTypes(ctList);
68
69 // 获取config下的files
70 List < Element > files = config.element( " files " ).elements();
71 Map < String,ConfigFile > filesMap = new HashMap < String,ConfigFile > ();
72 ConfigFile cf = null ;
73 for (Element e : files) {
74 cf = new ConfigFile();
75 String type = e.attributeValue( " type " );
76 cf.setType(type);
77 cf.setName(e.element( " name " ).getText());
78 cf.setSuffix(e.element( " suffix " ).getText());
79 cf.setCompress(e.element( " compress " ).getText());
80 cf.setCode(e.element( " code " ).getText());
81 cf.setSize(Long.parseLong(e.element( " size " ).getText()));
82 cf.setTempPath(e.element( " tempPath " ).getText());
83 cf.setPath(e.element( " path " ).getText());
84 filesMap.put(type, cf);
85 }
86 ifc.setFiles(filesMap);
87 lic.add(ifc);
88
89 }
90 Map < String,InterfaceConfig > msi = null ;
91 for (InterfaceConfig ifcs : lic){
92 if ( ! msmsi.containsKey(ifcs.getOrder())){
93 msi = new HashMap < String,InterfaceConfig > ();
94 msi.put(ifcs.getKey(), ifcs);
95 msmsi.put(ifcs.getOrder(),msi);
96 } else {
97 msmsi.get(ifcs.getOrder()).put(ifcs.getKey(), ifcs);
98 }
99 }
100
101 } catch (Exception e) {
102 throw new RuntimeException(e);
103 }
2 String path = InterfaceConfig. class .getResource( "" ).getPath().toString(); // 获取类所在路径
3 if (path.contains( " .jar " )) {
4 path = path.replace( " / " , File.separator); // 将/换成\
5 path = path.replace( " file: " , "" ); // 去掉file
6 path = path.replace( " classes\\ " , "" ); // 去掉classes\
7 if (path.startsWith( " \\ " )) {
8 path = path.substring( 1 ); // 去掉第一个\,如:、\D:\TongWeb

9 }
10 path = path.split( " WEB-INF " )[ 0 ] + " WEB-INF " + File.separator + " classes " ;
11 } else {
12 path = InterfaceConfig. class .getResource( " / " ).getPath().toString(); // 获取根路径
13 }
14 // String realPath = ActionUtils.getRequest().getRealPath("/")+File.separator+"WEB-INF"+File.separator+"classes";
15 File file = new File(path + File.separator + " InterfaceConfig.xml " );
16 SAXReader reader = new SAXReader();
17 Document doc = reader.read(file);
18 Element root = doc.getRootElement();
19 Iterator < Element > configs = root.elementIterator( " config " );
20 InterfaceConfig ifc = null ;
21 while (configs.hasNext()) {
22 ifc = new InterfaceConfig();
23 Element config = configs.next();
24
25 String key = config.attributeValue( " key " );
26 String name = config.attributeValue( " name " );
27 String order = config.attributeValue( " order " );
28 ifc.setKey(key);
29 ifc.setName(name);
30 ifc.setOrder(order);
31
32 // 获取config下的mode
33 String modeValue = config.element( " mode " ).getText();
34 ifc.setMode(modeValue);
35
36 // 获取初始抽取时间
37 String defaultTime = config.element( " defaultTime " ).getText();
38 ifc.setDefaultTime(defaultTime);
39
40 // 获取config下的parents
41 List < Element > parents = config.element( " parents " ).elements();
42 List < String > parentsList = new ArrayList < String > ();
43 for (Element e : parents) {
44 parentsList.add(e.getText());
45 }
46 ifc.setParents(parentsList);
47
48 // 获取config下的sqls
49 List < Element > sqls = config.element( " sqls " ).elements();
50 List < String > sqlsList = new ArrayList < String > ();
51 for (Element e : sqls) {
52 String sqlValue = e.getText().replaceAll( " \n " , " " ).replaceAll( " \t " , " " );
53 sqlsList.add(sqlValue);
54 }
55 ifc.setSqls(sqlsList);
56
57 // 获取config下的colTypes
58 List < Element > colTypes = config.element( " colTypes " ).elements();
59 List < ColType > ctList = new ArrayList < ColType > ();
60 ColType ct = null ;
61 for (Element e : colTypes) {
62 ct = new ColType();
63 ct.setType(e.element( " type " ).getText());
64 ct.setLength(e.element( " length " ).getText());
65 ctList.add(ct);
66 }
67 ifc.setColTypes(ctList);
68
69 // 获取config下的files
70 List < Element > files = config.element( " files " ).elements();
71 Map < String,ConfigFile > filesMap = new HashMap < String,ConfigFile > ();
72 ConfigFile cf = null ;
73 for (Element e : files) {
74 cf = new ConfigFile();
75 String type = e.attributeValue( " type " );
76 cf.setType(type);
77 cf.setName(e.element( " name " ).getText());
78 cf.setSuffix(e.element( " suffix " ).getText());
79 cf.setCompress(e.element( " compress " ).getText());
80 cf.setCode(e.element( " code " ).getText());
81 cf.setSize(Long.parseLong(e.element( " size " ).getText()));
82 cf.setTempPath(e.element( " tempPath " ).getText());
83 cf.setPath(e.element( " path " ).getText());
84 filesMap.put(type, cf);
85 }
86 ifc.setFiles(filesMap);
87 lic.add(ifc);
88
89 }
90 Map < String,InterfaceConfig > msi = null ;
91 for (InterfaceConfig ifcs : lic){
92 if ( ! msmsi.containsKey(ifcs.getOrder())){
93 msi = new HashMap < String,InterfaceConfig > ();
94 msi.put(ifcs.getKey(), ifcs);
95 msmsi.put(ifcs.getOrder(),msi);
96 } else {
97 msmsi.get(ifcs.getOrder()).put(ifcs.getKey(), ifcs);
98 }
99 }
100
101 } catch (Exception e) {
102 throw new RuntimeException(e);
103 }
先获取configs里面的所有config,然后对它进行迭代,获取每一个config。
接下来是获取config里面的节点了,如果config里的子节点没有“后代”了,如mode节点,那么直接String modeValue = config.element("mode").getText();就获取值了,
但是如果config里的有些子节点还有子节点,如files节点,就List<Element> files = config.element("files").elements();放到一个集合里,然后循环取出。