groovy构建和解析xml文件

原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/

代码示例:

构建xml文件:

 1 def static createXmlFile(){
 2     def sw = new StringWriter();
 3     def xml = new groovy.xml.MarkupBuilder(sw);
 4     xml.DataObject{
 5         def i = 0, j = 0;
 6         while(i<6){
 7             coal{
 8                 Color("#000000");//煤仓煤的颜色
 9                 tm("1");//更新煤仓数据的时间
10                 height("12");//煤仓煤位高度
11             }
12             i++;
13         }
14         while(j<6){
15             txt{
16                 name("煤仓名称");
17                 name("煤仓煤位");
18                 name("配煤煤种");
19                 name("煤位吨数");
20                 value("A仓");
21                 value("12m");
22                 value("高卡高硫煤");
23                 value("488.5t");
24             }
25             j++;
26         }
27     }
28         
29     def file = new File("web-app/shangmfa/config.xml");
30     if(file.exists()){//如果文件不存在则创建一个文件
31         file.mkdirs();
32     }
33     def xmlString = """
34         <?xml version='1.0' encoding='UTF-8'?>
35     """;
36     file.write(xmlString + sw.toString());
37 }

xml文件:

 1 <DataObject>
 2   <coal>
 3     <Color>#000000</Color>
 4     <tm>1</tm>
 5     <height>12</height>
 6   </coal>
 7   <coal>
 8     <Color>#000000</Color>
 9     <tm>1</tm>
10     <height>12</height>
11   </coal>
12   <coal>
13     <Color>#000000</Color>
14     <tm>1</tm>
15     <height>12</height>
16   </coal>
17   <coal>
18     <Color>#000000</Color>
19     <tm>1</tm>
20     <height>12</height>
21   </coal>
22   <coal>
23     <Color>#000000</Color>
24     <tm>1</tm>
25     <height>12</height>
26   </coal>
27   <coal>
28     <Color>#000000</Color>
29     <tm>1</tm>
30     <height>12</height>
31   </coal>
32   <txt>
33     <name>煤仓名称</name>
34     <name>煤仓煤位</name>
35     <name>配煤煤种</name>
36     <name>煤位吨数</name>
37     <value>A仓</value>
38     <value>12m</value>
39     <value>高卡高硫煤</value>
40     <value>488.5t</value>
41   </txt>
42   <txt>
43     <name>煤仓名称</name>
44     <name>煤仓煤位</name>
45     <name>配煤煤种</name>
46     <name>煤位吨数</name>
47     <value>A仓</value>
48     <value>12m</value>
49     <value>高卡高硫煤</value>
50     <value>488.5t</value>
51   </txt>
52   <txt>
53     <name>煤仓名称</name>
54     <name>煤仓煤位</name>
55     <name>配煤煤种</name>
56     <name>煤位吨数</name>
57     <value>A仓</value>
58     <value>12m</value>
59     <value>高卡高硫煤</value>
60     <value>488.5t</value>
61   </txt>
62   <txt>
63     <name>煤仓名称</name>
64     <name>煤仓煤位</name>
65     <name>配煤煤种</name>
66     <name>煤位吨数</name>
67     <value>A仓</value>
68     <value>12m</value>
69     <value>高卡高硫煤</value>
70     <value>488.5t</value>
71   </txt>
72   <txt>
73     <name>煤仓名称</name>
74     <name>煤仓煤位</name>
75     <name>配煤煤种</name>
76     <name>煤位吨数</name>
77     <value>A仓</value>
78     <value>12m</value>
79     <value>高卡高硫煤</value>
80     <value>488.5t</value>
81   </txt>
82   <txt>
83     <name>煤仓名称</name>
84     <name>煤仓煤位</name>
85     <name>配煤煤种</name>
86     <name>煤位吨数</name>
87     <value>A仓</value>
88     <value>12m</value>
89     <value>高卡高硫煤</value>
90     <value>488.5t</value>
91   </txt>
92 </DataObject>
原文地址:https://www.cnblogs.com/smallrock/p/3508343.html