dom4j 间隔插入节点 处理复杂的xml文档

前几周跟着老师做了个项目,个人主要负责xml文档处理.

先贴下修改前的xml(部分)

  1 <?xml version="1.0" encoding="GB2312" standalone="yes"?>
  2 <BroadcastData code="110000000" creationtime="20171103111425" version="2.0">
  3     <ProviderInfo id="ChinaEPG" name="David" />
  4     <SchedulerData>
  5         <Channel  type="Normal">
  6             <ChannelText language="chi">
  7                 <ChannelName>CCTV-1高清</ChannelName>
  8             </ChannelText>
  9             <Event eventid="1" begintime="20171103004800" duration="004500" eventtype="00">
 10                 <EventText language="chi">
 11                     <Name>动物世界</Name>
 12             <ShortDescription />
 13                 </EventText>
 14             </Event>
 15             <Event eventid="2" begintime="20171103013300" duration="003000" eventtype="11">
 16                 <EventText language="chi">
 17                     <Name>晚间新闻</Name>
 18             <ShortDescription />
 19                 </EventText>
 20             </Event>
 21             <Event eventid="3" begintime="20171103020300" duration="004800" eventtype="11">
 22                 <EventText language="chi">
 23                     <Name>超级工程Ⅱ:中国路</Name>
 24             <ShortDescription />
 25                 </EventText>
 26             </Event>
 27             <Event eventid="4" begintime="20171103025100" duration="000900" eventtype="11">
 28                 <EventText language="chi">
 29                     <Name>精彩1刻:航拍中国-陕西</Name>
 30             <ShortDescription />
 31                 </EventText>
 32             </Event>
 33             <Event eventid="5" begintime="20171103030000" duration="012900" eventtype="11">
 34                 <EventText language="chi">
 35                     <Name>中国诗词大会Ⅱ:第一场</Name>
 36             <ShortDescription />
 37                 </EventText>
 38             </Event>
 39             <Event eventid="6" begintime="20171103042900" duration="002800" eventtype="11">
 40                 <EventText language="chi">
 41                     <Name>今日说法</Name>
 42             <ShortDescription />
 43                 </EventText>
 44             </Event>
 45             <Event eventid="7" begintime="20171103045700" duration="003000" eventtype="11">
 46                 <EventText language="chi">
 47                     <Name>新闻联播</Name>
 48             <ShortDescription />
 49                 </EventText>
 50             </Event>
 51             <Event eventid="8" begintime="20171103052700" duration="003300" eventtype="11">
 52                 <EventText language="chi">
 53                     <Name>人与自然</Name>
 54             <ShortDescription />
 55                 </EventText>
 56             </Event>
 57             <Event eventid="9" begintime="20171103060000" duration="023600" eventtype="11">
 58                 <EventText language="chi">
 59                     <Name>朝闻天下</Name>
 60             <ShortDescription />
 61                 </EventText>
 62             </Event>
 63             <Event eventid="10" begintime="20171103083600" duration="004700" eventtype="11">
 64                 <EventText language="chi">
 65                     <Name>生活圈</Name>
 66             <ShortDescription />
 67                 </EventText>
 68             </Event>
 69             <Event eventid="11" begintime="20171103092300" duration="005100" eventtype="11">
 70                 <EventText language="chi">
 71                     <Name>太行山上(14)</Name>
 72             <ShortDescription />
 73                 </EventText>
 74             </Event>
 75             <Event eventid="12" begintime="20171103101400" duration="004700" eventtype="11">
 76                 <EventText language="chi">
 77                     <Name>太行山上(15)</Name>
 78             <ShortDescription />
 79                 </EventText>
 80             </Event>
 81             <Event eventid="13" begintime="20171103110100" duration="005900" eventtype="11">
 82                 <EventText language="chi">
 83                     <Name>太行山上(16)</Name>
 84             <ShortDescription />
 85                 </EventText>
 86             </Event>
 87             <Event eventid="14" begintime="20171103120000" duration="003600" eventtype="11">
 88                 <EventText language="chi">
 89                     <Name>新闻30分</Name>
 90             <ShortDescription />
 91                 </EventText>
 92             </Event>
 93             <Event eventid="15" begintime="20171103123600" duration="004200" eventtype="11">
 94                 <EventText language="chi">
 95                     <Name>今日说法</Name>
 96             <ShortDescription />
 97                 </EventText>
 98             </Event>
 99             <Event eventid="16" begintime="20171103131800" duration="004800" eventtype="11">
100                 <EventText language="chi">
101                     <Name>鸡毛飞上天(15)</Name>
102             <ShortDescription />
103                 </EventText>
104             </Event>
105             <Event eventid="17" begintime="20171103140600" duration="005300" eventtype="11">
106                 <EventText language="chi">
107                     <Name>鸡毛飞上天(16)</Name>
108             <ShortDescription />
109                 </EventText>
110             </Event>
111             <Event eventid="18" begintime="20171103145900" duration="005000" eventtype="11">
112                 <EventText language="chi">
113                     <Name>鸡毛飞上天(17)</Name>
114             <ShortDescription />
115                 </EventText>
116             </Event>
117             <Event eventid="19" begintime="20171103154900" duration="005700" eventtype="11">
118                 <EventText language="chi">
119                     <Name>鸡毛飞上天(18)</Name>
120             <ShortDescription />
121                 </EventText>
122             </Event>
123             <Event eventid="20" begintime="20171103164600" duration="000600" eventtype="11">
124                 <EventText language="chi">
125                     <Name>中国梦歌曲:满城烟花</Name>
126             <ShortDescription />
127                 </EventText>
128             </Event>
129             <Event eventid="21" begintime="20171103165200" duration="000800" eventtype="11">
130                 <EventText language="chi">
131                     <Name>第一动画乐园</Name>
132             <ShortDescription />
133                 </EventText>
134             </Event>
135             <Event eventid="22" begintime="20171103170000" duration="005700" eventtype="11">
136                 <EventText language="chi">
137                     <Name>第一动画乐园</Name>
138             <ShortDescription />
139                 </EventText>
140             </Event>
141             <Event eventid="23" begintime="20171103175700" duration="002600" eventtype="11">
142                 <EventText language="chi">
143                     <Name>第一动画乐园</Name>
144             <ShortDescription />
145                 </EventText>
146             </Event>
147             <Event eventid="24" begintime="20171103182300" duration="001000" eventtype="11">
148                 <EventText language="chi">
149                     <Name>第一动画乐园:故事乐园</Name>
150             <ShortDescription />
151                 </EventText>
152             </Event>
153             <Event eventid="25" begintime="20171103183300" duration="002700" eventtype="11">
154                 <EventText language="chi">
155                     <Name>第一动画乐园</Name>
156             <ShortDescription />
157                 </EventText>
158             </Event>
159             <Event eventid="26" begintime="20171103190000" duration="003000" eventtype="11">
160                 <EventText language="chi">
161                     <Name>新闻联播</Name>
162             <ShortDescription />
163                 </EventText>
164             </Event>
165             <Event eventid="27" begintime="20171103193000" duration="001000" eventtype="11">
166                 <EventText language="chi">
167                     <Name>天气预报</Name>
168             <ShortDescription />
169                 </EventText>
170             </Event>
171             <Event eventid="28" begintime="20171103194000" duration="002200" eventtype="11">
172                 <EventText language="chi">
173                     <Name>焦点访谈</Name>
174             <ShortDescription />
175                 </EventText>
176             </Event>
177             <Event eventid="29" begintime="20171103200200" duration="000500" eventtype="11">
178                 <EventText language="chi">
179                     <Name>前情提要:青恋(24)</Name>
180             <ShortDescription />
181                 </EventText>
182             </Event>
183             <Event eventid="30" begintime="20171103200700" duration="005300" eventtype="11">
184                 <EventText language="chi">
185                     <Name>青恋(24)</Name>
186             <ShortDescription />
187                 </EventText>
188             </Event>
189             <Event eventid="31" begintime="20171103210000" duration="010000" eventtype="11">
190                 <EventText language="chi">
191                     <Name>机智过人Ⅰ(6)</Name>
192             <ShortDescription />
193                 </EventText>
194             </Event>
195             <Event eventid="32" begintime="20171103220000" duration="004200" eventtype="11">
196                 <EventText language="chi">
197                     <Name>晚间新闻</Name>
198             <ShortDescription />
199                 </EventText>
200             </Event>
201             <Event eventid="33" begintime="20171103224200" duration="005600" eventtype="11">
202                 <EventText language="chi">
203                     <Name>机智过人Ⅰ(6)</Name>
204             <ShortDescription />
205                 </EventText>
206             </Event>
207             <Event eventid="34" begintime="20171103233800" duration="002100" eventtype="11">
208                 <EventText language="chi">
209                     <Name>加油向未来(9)</Name>
210             <ShortDescription />
211                 </EventText>
212             </Event>
213             <Event eventid="35" begintime="20171103235900" duration="012600" eventtype="22">
214                 <EventText language="chi">
215                     <Name>结束</Name>
216             <ShortDescription />
217                 </EventText>
218             </Event>
219             <Event eventid="36" begintime="20171104012500" duration="000800" eventtype="00">
220                 <EventText language="chi">
221                     <Name>生活提示</Name>
222             <ShortDescription />
223                 </EventText>
224             </Event>
225             <Event eventid="37" begintime="20171104013300" duration="003200" eventtype="11">
226                 <EventText language="chi">
227                     <Name>晚间新闻</Name>
228             <ShortDescription />
229                 </EventText>
230             </Event>
231             <Event eventid="38" begintime="20171104020500" duration="004500" eventtype="11">
232                 <EventText language="chi">
233                     <Name>动物世界</Name>
234             <ShortDescription />
235                 </EventText>
236             </Event>
237             <Event eventid="39" begintime="20171104025000" duration="001000" eventtype="11">
238                 <EventText language="chi">
239                     <Name>精彩1刻:航拍中国-陕西</Name>
240             <ShortDescription />
241                 </EventText>
242             </Event>
243             <Event eventid="40" begintime="20171104030000" duration="012900" eventtype="11">
244                 <EventText language="chi">
245                     <Name>中国诗词大会Ⅱ:第二场</Name>
246             <ShortDescription />
247                 </EventText>
248             </Event>
249             <Event eventid="41" begintime="20171104042900" duration="002800" eventtype="11">
250                 <EventText language="chi">
251                     <Name>今日说法</Name>
252             <ShortDescription />
253                 </EventText>
254             </Event>
255             <Event eventid="42" begintime="20171104045700" duration="003000" eventtype="11">
256                 <EventText language="chi">
257                     <Name>新闻联播</Name>
258             <ShortDescription />
259                 </EventText>
260             </Event>
261             <Event eventid="43" begintime="20171104052700" duration="003300" eventtype="11">
262                 <EventText language="chi">
263                     <Name>人与自然</Name>
264             <ShortDescription />
265                 </EventText>
266             </Event>
267             <Event eventid="44" begintime="20171104060000" duration="023600" eventtype="11">
268                 <EventText language="chi">
269                     <Name>朝闻天下</Name>
270             <ShortDescription />
271                 </EventText>
272             </Event>
273             <Event eventid="45" begintime="20171104083600" duration="004800" eventtype="11">
274                 <EventText language="chi">
275                     <Name>太行山上(17)</Name>
276             <ShortDescription />
277                 </EventText>
278             </Event>
279             <Event eventid="46" begintime="20171104092400" duration="004900" eventtype="11">
280                 <EventText language="chi">
281                     <Name>太行山上(18)</Name>
282             <ShortDescription />
283                 </EventText>
284             </Event>
285             <Event eventid="47" begintime="20171104101300" duration="014700" eventtype="11">
286                 <EventText language="chi">
287                     <Name>加油向未来(9)</Name>
288             <ShortDescription />
289                 </EventText>
290             </Event>
291             <Event eventid="48" begintime="20171104120000" duration="003600" eventtype="11">
292                 <EventText language="chi">
293                     <Name>新闻30分</Name>
294             <ShortDescription />
295                 </EventText>
296             </Event>
297             <Event eventid="49" begintime="20171104123600" duration="003900" eventtype="11">
298                 <EventText language="chi">
299                     <Name>今日说法</Name>
300             <ShortDescription />
301                 </EventText>
302             </Event>
303             <Event eventid="50" begintime="20171104131500" duration="010200" eventtype="11">
304                 <EventText language="chi">
305                     <Name>寻宝-我有传家宝</Name>
306             <ShortDescription />
307                 </EventText>
308             </Event>
309             <Event eventid="51" begintime="20171104141700" duration="005100" eventtype="11">
310                 <EventText language="chi">
311                     <Name>鸡毛飞上天(19)</Name>
312             <ShortDescription />
313                 </EventText>
314             </Event>
315             <Event eventid="52" begintime="20171104150800" duration="005000" eventtype="11">
316                 <EventText language="chi">
317                     <Name>鸡毛飞上天(20)</Name>
318             <ShortDescription />
319                 </EventText>
320             </Event>
321             <Event eventid="53" begintime="20171104155800" duration="004900" eventtype="11">
322                 <EventText language="chi">
323                     <Name>鸡毛飞上天(21)</Name>
324             <ShortDescription />
325                 </EventText>
326             </Event>
327             <Event eventid="54" begintime="20171104164700" duration="005700" eventtype="11">
328                 <EventText language="chi">
329                     <Name>鸡毛飞上天(22)</Name>
330             <ShortDescription />
331                 </EventText>
332             </Event>
333             <Event eventid="55" begintime="20171104174400" duration="001600" eventtype="11">
334                 <EventText language="chi">
335                     <Name>中国梦歌曲:大地芬芳</Name>
336             <ShortDescription />
337                 </EventText>
338             </Event>
339             <Event eventid="56" begintime="20171104180000" duration="010000" eventtype="11">
340                 <EventText language="chi">
341                     <Name>机智过人Ⅰ(6)</Name>
342             <ShortDescription />
343                 </EventText>
344             </Event>
345             <Event eventid="57" begintime="20171104190000" duration="003000" eventtype="11">
346                 <EventText language="chi">
347                     <Name>新闻联播</Name>
348             <ShortDescription />
349                 </EventText>
350             </Event>
351             <Event eventid="58" begintime="20171104193000" duration="000900" eventtype="11">
352                 <EventText language="chi">
353                     <Name>天气预报</Name>
354             <ShortDescription />
355                 </EventText>
356             </Event>
357             <Event eventid="59" begintime="20171104193900" duration="002400" eventtype="11">
358                 <EventText language="chi">
359                     <Name>焦点访谈</Name>
360             <ShortDescription />
361                 </EventText>
362             </Event>
363             <Event eventid="60" begintime="20171104200300" duration="015700" eventtype="11">
364                 <EventText language="chi">
365                     <Name>星光大道</Name>
366             <ShortDescription />
367                 </EventText>
368             </Event>
369             <Event eventid="61" begintime="20171104220000" duration="003900" eventtype="11">
370                 <EventText language="chi">
371                     <Name>晚间新闻</Name>
372             <ShortDescription />
373                 </EventText>
374             </Event>
375             <Event eventid="62" begintime="20171104223900" duration="010100" eventtype="11">
376                 <EventText language="chi">
377                     <Name>开讲啦</Name>
378             <ShortDescription />
379                 </EventText>
380             </Event>
381             <Event eventid="63" begintime="20171104234000" duration="001900" eventtype="11">
382                 <EventText language="chi">
383                     <Name>星光大道</Name>
384             <ShortDescription />
385                 </EventText>
386             </Event>
387             <Event eventid="64" begintime="20171104235900" duration="012400" eventtype="22">
388                 <EventText language="chi">
389                     <Name>结束</Name>
390             <ShortDescription />
391                 </EventText>
392             </Event>
View Code

修改后的xml(部分)

  1 <?xml version="1.0" encoding="GB2312" standalone="yes"?>
  2 <BroadcastData code="110000000" creationtime="20171103111425" version="2.0">
  3   <ProviderInfo id="ChinaEPG" name="David" />
  4   <SchedulerData>
  5     <Channel type="Normal">
  6       <ChannelText language="chi">
  7         <ChannelName>CCTV-1高清</ChannelName>
  8       </ChannelText>
  9       <Event eventid="03001" begintime="20171103004800" duration="004500" eventtype="00">
 10         <EventText language="chi">
 11           <Name>动物世界</Name>
 12           <ShortDescription />
 13         </EventText>
 14       </Event>
 15       <Event eventid="03002" begintime="20171103013300" duration="003000" eventtype="11">
 16         <EventText language="chi">
 17           <Name>晚间新闻</Name>
 18           <ShortDescription />
 19         </EventText>
 20       </Event>
 21       <Event eventid="03003" begintime="20171103020300" duration="004800" eventtype="11">
 22         <EventText language="chi">
 23           <Name>超级工程Ⅱ:中国路</Name>
 24           <ShortDescription />
 25         </EventText>
 26       </Event>
 27       <Event eventid="03004" begintime="20171103025100" duration="000900" eventtype="11">
 28         <EventText language="chi">
 29           <Name>精彩1刻:航拍中国-陕西</Name>
 30           <ShortDescription />
 31         </EventText>
 32       </Event>
 33       <Event eventid="03005" begintime="20171103030000" duration="012900" eventtype="11">
 34         <EventText language="chi">
 35           <Name>中国诗词大会Ⅱ:第一场</Name>
 36           <ShortDescription />
 37         </EventText>
 38       </Event>
 39       <Event eventid="03006" begintime="20171103042900" duration="002800" eventtype="11">
 40         <EventText language="chi">
 41           <Name>今日说法</Name>
 42           <ShortDescription />
 43         </EventText>
 44       </Event>
 45       <Event eventid="03007" begintime="20171103045700" duration="003000" eventtype="11">
 46         <EventText language="chi">
 47           <Name>新闻联播</Name>
 48           <ShortDescription />
 49         </EventText>
 50       </Event>
 51       <Event eventid="03008" begintime="20171103052700" duration="003200" eventtype="11">
 52         <EventText language="chi">
 53           <Name>人与自然</Name>
 54           <ShortDescription />
 55         </EventText>
 56       </Event>
 57       <Event begintime="20171103055900" eventtype="11" eventid="03009" duration="000100">
 58         <EventText language="chi">
 59           <Name>办中行ETC  免费送OBU设备</Name>
 60         </EventText>
 61       </Event>
 62       <Event eventid="03010" begintime="20171103060000" duration="023500" eventtype="11">
 63         <EventText language="chi">
 64           <Name>朝闻天下</Name>
 65           <ShortDescription />
 66         </EventText>
 67       </Event>
 68       <Event begintime="20171103083500" eventtype="11" eventid="03011" duration="000100">
 69         <EventText language="chi">
 70           <Name>办中行ETC  免费送OBU设备</Name>
 71         </EventText>
 72       </Event>
 73       <Event eventid="03012" begintime="20171103083600" duration="004600" eventtype="11">
 74         <EventText language="chi">
 75           <Name>生活圈</Name>
 76           <ShortDescription />
 77         </EventText>
 78       </Event>
 79       <Event begintime="20171103092200" eventtype="11" eventid="03013" duration="000100">
 80         <EventText language="chi">
 81           <Name>办中行ETC  免费送OBU设备</Name>
 82         </EventText>
 83       </Event>
 84       <Event eventid="03014" begintime="20171103092300" duration="005000" eventtype="11">
 85         <EventText language="chi">
 86           <Name>太行山上(14)</Name>
 87           <ShortDescription />
 88         </EventText>
 89       </Event>
 90       <Event begintime="20171103101300" eventtype="11" eventid="03015" duration="000100">
 91         <EventText language="chi">
 92           <Name>办中行ETC  免费送OBU设备</Name>
 93         </EventText>
 94       </Event>
 95       <Event eventid="03016" begintime="20171103101400" duration="004600" eventtype="11">
 96         <EventText language="chi">
 97           <Name>太行山上(15)</Name>
 98           <ShortDescription />
 99         </EventText>
100       </Event>
101       <Event begintime="20171103110000" eventtype="11" eventid="03017" duration="000100">
102         <EventText language="chi">
103           <Name>办中行ETC  免费送OBU设备</Name>
104         </EventText>
105       </Event>
106       <Event eventid="03018" begintime="20171103110100" duration="005800" eventtype="11">
107         <EventText language="chi">
108           <Name>太行山上(16)</Name>
109           <ShortDescription />
110         </EventText>
111       </Event>
112       <Event begintime="20171103115900" eventtype="11" eventid="03019" duration="000100">
113         <EventText language="chi">
114           <Name>办中行ETC  免费送OBU设备</Name>
115         </EventText>
116       </Event>
117       <Event eventid="03020" begintime="20171103120000" duration="003500" eventtype="11">
118         <EventText language="chi">
119           <Name>新闻30分</Name>
120           <ShortDescription />
121         </EventText>
122       </Event>
123       <Event begintime="20171103123500" eventtype="11" eventid="03021" duration="000100">
124         <EventText language="chi">
125           <Name>办中行ETC  免费送OBU设备</Name>
126         </EventText>
127       </Event>
128       <Event eventid="03022" begintime="20171103123600" duration="004100" eventtype="11">
129         <EventText language="chi">
130           <Name>今日说法</Name>
131           <ShortDescription />
132         </EventText>
133       </Event>
134       <Event begintime="20171103131700" eventtype="11" eventid="03023" duration="000100">
135         <EventText language="chi">
136           <Name>办中行ETC  免费送OBU设备</Name>
137         </EventText>
138       </Event>
139       <Event eventid="03024" begintime="20171103131800" duration="004700" eventtype="11">
140         <EventText language="chi">
141           <Name>鸡毛飞上天(15)</Name>
142           <ShortDescription />
143         </EventText>
144       </Event>
145       <Event begintime="20171103140500" eventtype="11" eventid="03025" duration="000100">
146         <EventText language="chi">
147           <Name>办中行ETC  免费送OBU设备</Name>
148         </EventText>
149       </Event>
150       <Event eventid="03026" begintime="20171103140600" duration="005200" eventtype="11">
151         <EventText language="chi">
152           <Name>鸡毛飞上天(16)</Name>
153           <ShortDescription />
154         </EventText>
155       </Event>
156       <Event begintime="20171103145800" eventtype="11" eventid="03027" duration="000100">
157         <EventText language="chi">
158           <Name>办中行ETC  免费送OBU设备</Name>
159         </EventText>
160       </Event>
161       <Event eventid="03028" begintime="20171103145900" duration="004900" eventtype="11">
162         <EventText language="chi">
163           <Name>鸡毛飞上天(17)</Name>
164           <ShortDescription />
165         </EventText>
166       </Event>
167       <Event begintime="20171103154800" eventtype="11" eventid="03029" duration="000100">
168         <EventText language="chi">
169           <Name>办中行ETC  免费送OBU设备</Name>
170         </EventText>
171       </Event>
172       <Event eventid="03030" begintime="20171103154900" duration="005600" eventtype="11">
173         <EventText language="chi">
174           <Name>鸡毛飞上天(18)</Name>
175           <ShortDescription />
176         </EventText>
177       </Event>
178       <Event begintime="20171103164500" eventtype="11" eventid="03031" duration="000100">
179         <EventText language="chi">
180           <Name>办中行ETC  免费送OBU设备</Name>
181         </EventText>
182       </Event>
183       <Event eventid="03032" begintime="20171103164600" duration="000500" eventtype="11">
184         <EventText language="chi">
185           <Name>中国梦歌曲:满城烟花</Name>
186           <ShortDescription />
187         </EventText>
188       </Event>
189       <Event begintime="20171103165100" eventtype="11" eventid="03033" duration="000100">
190         <EventText language="chi">
191           <Name>办中行ETC  免费送OBU设备</Name>
192         </EventText>
193       </Event>
194       <Event eventid="03034" begintime="20171103165200" duration="000700" eventtype="11">
195         <EventText language="chi">
196           <Name>第一动画乐园</Name>
197           <ShortDescription />
198         </EventText>
199       </Event>
200       <Event begintime="20171103165900" eventtype="11" eventid="03035" duration="000100">
201         <EventText language="chi">
202           <Name>办中行ETC  免费送OBU设备</Name>
203         </EventText>
204       </Event>
205       <Event eventid="03036" begintime="20171103170000" duration="005600" eventtype="11">
206         <EventText language="chi">
207           <Name>第一动画乐园</Name>
208           <ShortDescription />
209         </EventText>
210       </Event>
211       <Event begintime="20171103175600" eventtype="11" eventid="03037" duration="000100">
212         <EventText language="chi">
213           <Name>办中行ETC  免费送OBU设备</Name>
214         </EventText>
215       </Event>
216       <Event eventid="03038" begintime="20171103175700" duration="002500" eventtype="11">
217         <EventText language="chi">
218           <Name>第一动画乐园</Name>
219           <ShortDescription />
220         </EventText>
221       </Event>
222       <Event begintime="20171103182200" eventtype="11" eventid="03039" duration="000100">
223         <EventText language="chi">
224           <Name>办中行ETC  免费送OBU设备</Name>
225         </EventText>
226       </Event>
227       <Event eventid="03040" begintime="20171103182300" duration="000900" eventtype="11">
228         <EventText language="chi">
229           <Name>第一动画乐园:故事乐园</Name>
230           <ShortDescription />
231         </EventText>
232       </Event>
233       <Event begintime="20171103183200" eventtype="11" eventid="03041" duration="000100">
234         <EventText language="chi">
235           <Name>办中行ETC  免费送OBU设备</Name>
236         </EventText>
237       </Event>
238       <Event eventid="03042" begintime="20171103183300" duration="002600" eventtype="11">
239         <EventText language="chi">
240           <Name>第一动画乐园</Name>
241           <ShortDescription />
242         </EventText>
243       </Event>
244       <Event begintime="20171103185900" eventtype="11" eventid="03043" duration="000100">
245         <EventText language="chi">
246           <Name>办中行ETC  免费送OBU设备</Name>
247         </EventText>
248       </Event>
249       <Event eventid="03044" begintime="20171103190000" duration="002900" eventtype="11">
250         <EventText language="chi">
251           <Name>新闻联播</Name>
252           <ShortDescription />
253         </EventText>
254       </Event>
255       <Event begintime="20171103192900" eventtype="11" eventid="03045" duration="000100">
256         <EventText language="chi">
257           <Name>办中行ETC  免费送OBU设备</Name>
258         </EventText>
259       </Event>
260       <Event eventid="03046" begintime="20171103193000" duration="000900" eventtype="11">
261         <EventText language="chi">
262           <Name>天气预报</Name>
263           <ShortDescription />
264         </EventText>
265       </Event>
266       <Event begintime="20171103193900" eventtype="11" eventid="03047" duration="000100">
267         <EventText language="chi">
268           <Name>办中行ETC  免费送OBU设备</Name>
269         </EventText>
270       </Event>
271       <Event eventid="03048" begintime="20171103194000" duration="002100" eventtype="11">
272         <EventText language="chi">
273           <Name>焦点访谈</Name>
274           <ShortDescription />
275         </EventText>
276       </Event>
277       <Event begintime="20171103200100" eventtype="11" eventid="03049" duration="000100">
278         <EventText language="chi">
279           <Name>办中行ETC  免费送OBU设备</Name>
280         </EventText>
281       </Event>
282       <Event eventid="03050" begintime="20171103200200" duration="000400" eventtype="11">
283         <EventText language="chi">
284           <Name>前情提要:青恋(24)</Name>
285           <ShortDescription />
286         </EventText>
287       </Event>
288       <Event begintime="20171103200600" eventtype="11" eventid="03051" duration="000100">
289         <EventText language="chi">
290           <Name>办中行ETC  免费送OBU设备</Name>
291         </EventText>
292       </Event>
293       <Event eventid="03052" begintime="20171103200700" duration="005200" eventtype="11">
294         <EventText language="chi">
295           <Name>青恋(24)</Name>
296           <ShortDescription />
297         </EventText>
298       </Event>
299       <Event begintime="20171103205900" eventtype="11" eventid="03053" duration="000100">
300         <EventText language="chi">
301           <Name>办中行ETC  免费送OBU设备</Name>
302         </EventText>
303       </Event>
304       <Event eventid="03054" begintime="20171103210000" duration="005900" eventtype="11">
305         <EventText language="chi">
306           <Name>机智过人Ⅰ(6)</Name>
307           <ShortDescription />
308         </EventText>
309       </Event>
310       <Event begintime="20171103215900" eventtype="11" eventid="03055" duration="000100">
311         <EventText language="chi">
312           <Name>办中行ETC  免费送OBU设备</Name>
313         </EventText>
314       </Event>
315       <Event eventid="03056" begintime="20171103220000" duration="004100" eventtype="11">
316         <EventText language="chi">
317           <Name>晚间新闻</Name>
318           <ShortDescription />
319         </EventText>
320       </Event>
321       <Event begintime="20171103224100" eventtype="11" eventid="03057" duration="000100">
322         <EventText language="chi">
323           <Name>办中行ETC  免费送OBU设备</Name>
324         </EventText>
325       </Event>
326       <Event eventid="03058" begintime="20171103224200" duration="005500" eventtype="11">
327         <EventText language="chi">
328           <Name>机智过人Ⅰ(6)</Name>
329           <ShortDescription />
330         </EventText>
331       </Event>
332       <Event begintime="20171103233700" eventtype="11" eventid="03059" duration="000100">
333         <EventText language="chi">
334           <Name>办中行ETC  免费送OBU设备</Name>
335         </EventText>
336       </Event>
337       <Event eventid="03060" begintime="20171103233800" duration="002000" eventtype="11">
338         <EventText language="chi">
339           <Name>加油向未来(9)</Name>
340           <ShortDescription />
341         </EventText>
342       </Event>
343       <Event begintime="20171103235800" eventtype="11" eventid="03061" duration="000100">
344         <EventText language="chi">
345           <Name>办中行ETC  免费送OBU设备</Name>
346         </EventText>
347       </Event>
348       <Event eventid="03062" begintime="20171103235900" duration="012600" eventtype="22">
349         <EventText language="chi">
350           <Name>结束</Name>
351           <ShortDescription />
352         </EventText>
353       </Event>
354       <Event eventid="04001" begintime="20171104012500" duration="000800" eventtype="00">
355         <EventText language="chi">
356           <Name>生活提示</Name>
357           <ShortDescription />
358         </EventText>
359       </Event>
360       <Event eventid="04002" begintime="20171104013300" duration="003200" eventtype="11">
361         <EventText language="chi">
362           <Name>晚间新闻</Name>
363           <ShortDescription />
364         </EventText>
365       </Event>
366       <Event eventid="04003" begintime="20171104020500" duration="004500" eventtype="11">
367         <EventText language="chi">
368           <Name>动物世界</Name>
369           <ShortDescription />
370         </EventText>
371       </Event>
372       <Event eventid="04004" begintime="20171104025000" duration="001000" eventtype="11">
373         <EventText language="chi">
374           <Name>精彩1刻:航拍中国-陕西</Name>
375           <ShortDescription />
376         </EventText>
377       </Event>
378       <Event eventid="04005" begintime="20171104030000" duration="012900" eventtype="11">
379         <EventText language="chi">
380           <Name>中国诗词大会Ⅱ:第二场</Name>
381           <ShortDescription />
382         </EventText>
383       </Event>
384       <Event eventid="04006" begintime="20171104042900" duration="002800" eventtype="11">
385         <EventText language="chi">
386           <Name>今日说法</Name>
387           <ShortDescription />
388         </EventText>
389       </Event>
390       <Event eventid="04007" begintime="20171104045700" duration="003000" eventtype="11">
391         <EventText language="chi">
392           <Name>新闻联播</Name>
393           <ShortDescription />
394         </EventText>
395       </Event>
396       <Event eventid="04008" begintime="20171104052700" duration="003200" eventtype="11">
397         <EventText language="chi">
398           <Name>人与自然</Name>
399           <ShortDescription />
400         </EventText>
401       </Event>
402       <Event begintime="20171104055900" eventtype="11" eventid="04009" duration="000100">
403         <EventText language="chi">
404           <Name>办中行ETC  免费送OBU设备</Name>
405         </EventText>
406       </Event>
407       <Event eventid="04010" begintime="20171104060000" duration="023500" eventtype="11">
408         <EventText language="chi">
409           <Name>朝闻天下</Name>
410           <ShortDescription />
411         </EventText>
412       </Event>
413       <Event begintime="20171104083500" eventtype="11" eventid="04011" duration="000100">
414         <EventText language="chi">
415           <Name>办中行ETC  免费送OBU设备</Name>
416         </EventText>
417       </Event>
418       <Event eventid="04012" begintime="20171104083600" duration="004700" eventtype="11">
419         <EventText language="chi">
420           <Name>太行山上(17)</Name>
421           <ShortDescription />
422         </EventText>
423       </Event>
424       <Event begintime="20171104092300" eventtype="11" eventid="04013" duration="000100">
425         <EventText language="chi">
426           <Name>办中行ETC  免费送OBU设备</Name>
427         </EventText>
428       </Event>
429       <Event eventid="04014" begintime="20171104092400" duration="004800" eventtype="11">
430         <EventText language="chi">
431           <Name>太行山上(18)</Name>
432           <ShortDescription />
433         </EventText>
434       </Event>
435       <Event begintime="20171104101200" eventtype="11" eventid="04015" duration="000100">
436         <EventText language="chi">
437           <Name>办中行ETC  免费送OBU设备</Name>
438         </EventText>
439       </Event>
440       <Event eventid="04016" begintime="20171104101300" duration="014600" eventtype="11">
441         <EventText language="chi">
442           <Name>加油向未来(9)</Name>
443           <ShortDescription />
444         </EventText>
445       </Event>
446       <Event begintime="20171104115900" eventtype="11" eventid="04017" duration="000100">
447         <EventText language="chi">
448           <Name>办中行ETC  免费送OBU设备</Name>
449         </EventText>
450       </Event>
451       <Event eventid="04018" begintime="20171104120000" duration="003500" eventtype="11">
452         <EventText language="chi">
453           <Name>新闻30分</Name>
454           <ShortDescription />
455         </EventText>
456       </Event>
457       <Event begintime="20171104123500" eventtype="11" eventid="04019" duration="000100">
458         <EventText language="chi">
459           <Name>办中行ETC  免费送OBU设备</Name>
460         </EventText>
461       </Event>
462       <Event eventid="04020" begintime="20171104123600" duration="003800" eventtype="11">
463         <EventText language="chi">
464           <Name>今日说法</Name>
465           <ShortDescription />
466         </EventText>
467       </Event>
468       <Event begintime="20171104131400" eventtype="11" eventid="04021" duration="000100">
469         <EventText language="chi">
470           <Name>办中行ETC  免费送OBU设备</Name>
471         </EventText>
472       </Event>
473       <Event eventid="04022" begintime="20171104131500" duration="010100" eventtype="11">
474         <EventText language="chi">
475           <Name>寻宝-我有传家宝</Name>
476           <ShortDescription />
477         </EventText>
478       </Event>
479       <Event begintime="20171104141600" eventtype="11" eventid="04023" duration="000100">
480         <EventText language="chi">
481           <Name>办中行ETC  免费送OBU设备</Name>
482         </EventText>
483       </Event>
484       <Event eventid="04024" begintime="20171104141700" duration="005000" eventtype="11">
485         <EventText language="chi">
486           <Name>鸡毛飞上天(19)</Name>
487           <ShortDescription />
488         </EventText>
489       </Event>
490       <Event begintime="20171104150700" eventtype="11" eventid="04025" duration="000100">
491         <EventText language="chi">
492           <Name>办中行ETC  免费送OBU设备</Name>
493         </EventText>
494       </Event>
495       <Event eventid="04026" begintime="20171104150800" duration="004900" eventtype="11">
496         <EventText language="chi">
497           <Name>鸡毛飞上天(20)</Name>
498           <ShortDescription />
499         </EventText>
500       </Event>
501       <Event begintime="20171104155700" eventtype="11" eventid="04027" duration="000100">
502         <EventText language="chi">
503           <Name>办中行ETC  免费送OBU设备</Name>
504         </EventText>
505       </Event>
506       <Event eventid="04028" begintime="20171104155800" duration="004800" eventtype="11">
507         <EventText language="chi">
508           <Name>鸡毛飞上天(21)</Name>
509           <ShortDescription />
510         </EventText>
511       </Event>
512       <Event begintime="20171104164600" eventtype="11" eventid="04029" duration="000100">
513         <EventText language="chi">
514           <Name>办中行ETC  免费送OBU设备</Name>
515         </EventText>
516       </Event>
517       <Event eventid="04030" begintime="20171104164700" duration="005600" eventtype="11">
518         <EventText language="chi">
519           <Name>鸡毛飞上天(22)</Name>
520           <ShortDescription />
521         </EventText>
522       </Event>
523       <Event begintime="20171104174300" eventtype="11" eventid="04031" duration="000100">
524         <EventText language="chi">
525           <Name>办中行ETC  免费送OBU设备</Name>
526         </EventText>
527       </Event>
528       <Event eventid="04032" begintime="20171104174400" duration="001500" eventtype="11">
529         <EventText language="chi">
530           <Name>中国梦歌曲:大地芬芳</Name>
531           <ShortDescription />
532         </EventText>
533       </Event>
534       <Event begintime="20171104175900" eventtype="11" eventid="04033" duration="000100">
535         <EventText language="chi">
536           <Name>办中行ETC  免费送OBU设备</Name>
537         </EventText>
538       </Event>
539       <Event eventid="04034" begintime="20171104180000" duration="005900" eventtype="11">
540         <EventText language="chi">
541           <Name>机智过人Ⅰ(6)</Name>
542           <ShortDescription />
543         </EventText>
544       </Event>
545       <Event begintime="20171104185900" eventtype="11" eventid="04035" duration="000100">
546         <EventText language="chi">
547           <Name>办中行ETC  免费送OBU设备</Name>
548         </EventText>
549       </Event>
550       <Event eventid="04036" begintime="20171104190000" duration="002900" eventtype="11">
551         <EventText language="chi">
552           <Name>新闻联播</Name>
553           <ShortDescription />
554         </EventText>
555       </Event>
556       <Event begintime="20171104192900" eventtype="11" eventid="04037" duration="000100">
557         <EventText language="chi">
558           <Name>办中行ETC  免费送OBU设备</Name>
559         </EventText>
560       </Event>
561       <Event eventid="04038" begintime="20171104193000" duration="000800" eventtype="11">
562         <EventText language="chi">
563           <Name>天气预报</Name>
564           <ShortDescription />
565         </EventText>
566       </Event>
567       <Event begintime="20171104193800" eventtype="11" eventid="04039" duration="000100">
568         <EventText language="chi">
569           <Name>办中行ETC  免费送OBU设备</Name>
570         </EventText>
571       </Event>
572       <Event eventid="04040" begintime="20171104193900" duration="002300" eventtype="11">
573         <EventText language="chi">
574           <Name>焦点访谈</Name>
575           <ShortDescription />
576         </EventText>
577       </Event>
578       <Event begintime="20171104200200" eventtype="11" eventid="04041" duration="000100">
579         <EventText language="chi">
580           <Name>办中行ETC  免费送OBU设备</Name>
581         </EventText>
582       </Event>
583       <Event eventid="04042" begintime="20171104200300" duration="015600" eventtype="11">
584         <EventText language="chi">
585           <Name>星光大道</Name>
586           <ShortDescription />
587         </EventText>
588       </Event>
589       <Event begintime="20171104215900" eventtype="11" eventid="04043" duration="000100">
590         <EventText language="chi">
591           <Name>办中行ETC  免费送OBU设备</Name>
592         </EventText>
593       </Event>
594       <Event eventid="04044" begintime="20171104220000" duration="003800" eventtype="11">
595         <EventText language="chi">
596           <Name>晚间新闻</Name>
597           <ShortDescription />
598         </EventText>
599       </Event>
600       <Event begintime="20171104223800" eventtype="11" eventid="04045" duration="000100">
601         <EventText language="chi">
602           <Name>办中行ETC  免费送OBU设备</Name>
603         </EventText>
604       </Event>
605       <Event eventid="04046" begintime="20171104223900" duration="010000" eventtype="11">
606         <EventText language="chi">
607           <Name>开讲啦</Name>
608           <ShortDescription />
609         </EventText>
610       </Event>
611       <Event begintime="20171104233900" eventtype="11" eventid="04047" duration="000100">
612         <EventText language="chi">
613           <Name>办中行ETC  免费送OBU设备</Name>
614         </EventText>
615       </Event>
616       <Event eventid="04048" begintime="20171104234000" duration="001800" eventtype="11">
617         <EventText language="chi">
618           <Name>星光大道</Name>
619           <ShortDescription />
620         </EventText>
621       </Event>
622       <Event begintime="20171104235800" eventtype="11" eventid="04049" duration="000100">
623         <EventText language="chi">
624           <Name>办中行ETC  免费送OBU设备</Name>
625         </EventText>
626       </Event>
627       <Event eventid="04050" begintime="20171104235900" duration="012400" eventtype="22">
628         <EventText language="chi">
629           <Name>结束</Name>
630           <ShortDescription />
631         </EventText>
632       </Event>
View Code

说明:  1.修改后的xml间隔插入了广告,如从每天的6点开始接下来每天6点都要插入广告

2.eventid,begintime,duration进行了变化如eventid = 1 ----------> 03001

3.原xml的eventid每一天是相连的,即第一天结束eventid是35,第二天开始的eventid是36但是

修改后的eventid是每天从1开始

4.广告的持续时间是确定的


个人思路:插入广告后再进行属性的调整

知识点1:间隔插入节点

插入节点之前需要准备好用到的adEvent(广告节点)我已经提前创建,已经插入广告的时间点(timeIndex),后面会给出对应的方法

最初的想法是遍历节点的集合进行i+=2的操作,突然发现这种想法的致命缺陷在于间隔插入要求i+=2和遍历要求的i++互相冲突,于是开始思考采用分组的方式来避免

仔细观察了下xml文档,发现可以用每天的结束标签进行分组,于是先获得结束标签的索引,然后遍历每个组进行插入,有人可能问了,这样不还是冲突.实际上这个时候的遍历和间隔插入已经不再使用同一个参数i了所以不再有冲突

注意:1.使用了java集合的sublist(...)方法进行集合的截取操作

2.要使用Element对象的clone方法,否则会报 The Content already has an existing parent xxx的异常

 1     //获得结束标签的索引
 2             List<Integer> endIndex = new ArrayList<Integer>();
 3             for(int i=1; i<events.size(); i++){
 4                 if("22".equals(events.get(i).attributeValue("eventtype"))) {
 5                     endIndex.add(i);
 6                 }
 7             }
 8             System.out.println();
 9             //分组处理  跳过第一个channelText
10             List<Element> list = new ArrayList<Element>();
11             int count = 0;
12             if(timeIndex <=endIndex.get(0)) {
13                 for(int i=0;i<endIndex.size(); i++) {
14                     if(i == 0) {
15                         list = events.subList(1,endIndex.get(i)+1);
16                         //插入广告
17                         for(int j=timeIndex; j<list.size();j+=2) {
18                             list.add(j,(Element)adEvent.clone());
19                             count++;//计算插入了多少个广告
20                         }
21                     }else {
22                         list = events.subList(endIndex.get(i-1)+1+count,endIndex.get(i)+1+count);
23                         for(int j=timeIndex; j<list.size();j+=2) {
24                             list.add(j,(Element)adEvent.clone());
25                             count++;
26                         }
27                     }
28                 }
29             }else {
30                 //为了应对插入的节点不从第一天开始
31                 for(int i=0;i<endIndex.size()-1; i++) {
32                     int temp1 = timeIndex - endIndex.get(i);
33                     int temp2 = timeIndex - endIndex.get(i+1);
34                     if(temp1 >0 && temp2 <0) {
35                         /*System.out.println("temp1----|" + temp1);
36                         System.out.println("temp2-----|" + temp2);*/
37                         for(int j=i+1;j<endIndex.size();j++) {
38                             list = events.subList(endIndex.get(j-1)+1+count,endIndex.get(j)+1+count);
39                             for(int m= temp1; m<list.size()-1;m+=2) {
40                                 list.add(m,(Element)adEvent.clone());
41                                 count++;
42                             }
43                         }
44                     }
45                 }
46                 
47             }

以下的方法涉及的参数Map<String,String> propertiesMap是封装了的配置文件的信息

获得timeIndex的方法

 1 /**
 2      * 
 3      * @param beginTime  -- 广告的开始时间点
 4      * @return 该时间点的索引
 5      */
 6     public  static int getTimeIndex(Document document,String beginTime) {
 7         List<Element> events = document.getRootElement()
 8                                 .element("SchedulerData")
 9                                 .element("Channel").elements();
10         System.out.println("===" + beginTime);
11         Long long_beginTime1 = Long.parseLong(beginTime);
12         for(int i=1;i<events.size()-1; i++) {
13             if(beginTime.equals(events.get(i).attributeValue("begintime"))) {
14                 return i;
15             }else {
16                 Long long_beginTime2  = Long.parseLong(events.get(i).attributeValue("begintime"));
17                 Long long_beginTime3  = Long.parseLong(events.get(i+1).attributeValue("begintime"));
18                 Long duration = Long.parseLong(events.get(i).attributeValue("duration"));
19                 Long temp1 = long_beginTime1 - long_beginTime2;
20                 Long temp2 = long_beginTime1 - long_beginTime3;
21                 if(temp1 > 0 && temp2 <0){
22                     return i;
23                 }
24             }
25         }
26         return 0;
27     }
View Code

使用timeIndex时要特别注意,一旦timeIndex为0说明节目的日期变了,此时要校对广告的开始时间

1 if(timeIndex == 0) {
2                 String begin = events.get(1).attributeValue("begintime");
3                 beginTime = beginTime.replace(beginTime.substring(0,8),begin.substring(0,8));
4                 timeIndex = getTimeIndex(document, beginTime);
5             }

创建adEvent的方法,调用的时候是从数据库读取广告信息进行创建

 1 public static Element createAdElement(Ad ad,String time,Map<String,String> propertiesMap) {
 2         if(ad == null) {
 3             System.out.println("广告对象为空!");
 4             return null;
 5         }
 6         Element adElement = DocumentHelper.createElement("Event");
 7         //beginTime
 8         if(ad.getBeginDate() != null && time != null) {
 9             String beginTime = ad.getBeginTime().toString();
10             beginTime = beginTime.replace(":","");
11             beginTime = beginTime.substring(0,4);
12             beginTime = time.substring(0,8) + beginTime + "00";// --->20171103083500
13             adElement.addAttribute("begintime",beginTime);
14         }
15         //duration
16         String duration = ad.getDuration();
17         if(duration != null) {
18             adElement.addAttribute("duration",duration);
19         }
20         //eventtype
21         String adEventType = propertiesMap.get("adEventType");
22         if(adEventType != null) {
23             adElement.addAttribute("eventtype",adEventType);
24         }
25         
26         //eventid
27         String eventId = "";
28         adElement.addAttribute("eventid",eventId);
29         
30         //language
31         String adLanguage ="chi";
32         Element eventText = adElement.addElement("EventText").addAttribute("language",adLanguage);
33         
34         //name节点
35         if(eventText != null) {
36             Element name = eventText.addElement("name");
37             if(ad.getContent() != null) {
38                 name.setText(ad.getContent());
39             }
40         }
41         return adElement;
42     }
View Code

知识点2 更新受到广告影响的节目的开始时间(begintime)以及持续时间(duration)

说明:此方法较为简单,只需判断下一个节点是不是广告从而进行相应的调整即可

 1 /**
 2      * 修改广告的begintime信息与受到广告影响的节目duration
 3      * @param events   
 4      */
 5     private void updateAdtime(List<Element> events,String adDuration) {
 6         for(int i=2;i<events.size();i++) {
 7             Attribute duration = events.get(i).attribute("duration");
 8             if(adDuration != null) {
 9                 if(duration.getText().equals(adDuration)) {
10                     String lastNodeBegin = events.get(i-1).attributeValue("begintime");
11                     String lastNodeDuration = events.get(i-1).attributeValue("duration");
12                     if(lastNodeBegin == null || lastNodeDuration == null) {
13                         System.out.println("上个节点不存在");
14                         return;
15                     }
16                     //修改节目的持续时间
17                     Long temp = Long.parseLong(lastNodeDuration);
18                     temp = temp - Long.parseLong(duration.getText());
19                     events.get(i-1).attribute("duration").setText(temp.toString());
20                     
21                     //修改后重新获取
22                     lastNodeBegin = events.get(i-1).attributeValue("begintime");
23                     lastNodeDuration = events.get(i-1).attributeValue("duration");
24                     
25                     //设置广告的开始时间
26                     Long adBeginTime = Long.parseLong(lastNodeBegin) + Long.parseLong(lastNodeDuration);
27                     String strAdBegin = adBeginTime.toString();
28                     Long min = Long.parseLong(strAdBegin.substring(strAdBegin.length()-4));
29                     if( min >= 6000) {
30                         adBeginTime = adBeginTime + 10000 -6000;
31                     }
32                     strAdBegin = adBeginTime.toString();
33                     events.get(i).attribute("begintime").setText(strAdBegin);
34                 }    
35             }
36         }
37         
38     }
View Code

知识点3 更新eventid,以1103为例1---->03001,  10------>03010

注意:id和begintime的dd相关,注意判断原id的长度

/**
     * 修改id
     * 1-->03001  10--->03010 
     */
    private void updateId(List<Element> events) {
        if(events.size() == 0) {
            return;
        }
        List<List<Element>> groups = new ArrayList<List<Element>>();
         for(int i=1; i<events.size(); i++){
             Attribute oldId = events.get(i).attribute("eventid");
                 String time = events.get(i).attributeValue("begintime");
                String sb = "";
                 if(time != null){
                     sb = time.substring(6,8);    // 截取dd
                 }
                 if(i<10) {
                     sb += "00" + i;
                 }else if(i<100) {
                     sb += "0" + i;
                 }
                 if(oldId != null){
                    oldId.setText(sb);
                 }
             if("22".equals(events.get(i).attributeValue("eventtype"))) {
                 if(events.size()> (i+1)) {
                     List<Element> list = events.subList(i+1,events.size());
                     groups.add(list);
                 }
             }
         }
         for(int i=0; i<groups.size(); i++) {
             List<Element> subList = groups.get(i);
             for(int j=0; j<subList.size(); j++) {
                 Attribute oldId = subList.get(j).attribute("eventid");
                 String time = subList.get(j).attributeValue("begintime");
                    String sb = "";
                     if(time != null){
                         sb = time.substring(6,8);    // 截取dd
                     }
                     if(j<10) {
                         sb += "00" + (j+1);
                     }else if(j<100) {
                         sb += "0" + (j+1);
                     }
                     if(oldId != null){
                        oldId.setText(sb);
                     }
             }
         }
    }
    
View Code

 

知识点4 生成修改后的xml

 1 /**
 2      * 将修改后的xml文档重新生成  
 3      * @param document
 4      * @param path
 5      * @param encode
 6      */
 7     public void write(Document document,String path,Map<String,String> propertiesMap) {
 8             String channelEncoding = propertiesMap.get("channelEncoding");
 9             if(channelEncoding != null) {
10                 format.setEncoding(channelEncoding);
11             }
12             format.setNewLineAfterDeclaration(false);
13             File file;
14         try {
15             file = new File(path);
16             if(file.getParentFile().exists()) {
17                 file.createNewFile();
18             }else {
19                 file.getParentFile().mkdirs();
20                 file.createNewFile();
21             }
22             XMLWriter write = new XMLWriter(new FileOutputStream(file,false),format);
23             write.write(document);
24             write.close();
25             } catch (IOException e) {
26                 e.printStackTrace();
27             }
28     }

 

posted @ 2017-11-16 22:33  tele  阅读(467)  评论(0编辑  收藏  举报