解析XML

 1. 介绍

1)DOM(JAXP Crimson解析器) 
        DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准。DOM是以层次结构组织的节点或信息片断的集合。这个层次结构允许开发人员在树中寻找特定信息。分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作。由于它是基于信息层次的,因而DOM被认为是基于树或基于对象的。DOM以及广义的基于树的处理具有几个优点。首先,由于树在内存中是持久的,因此可以修改它以便应用程序能对数据和结构作出更改。它还可以在任何时候在树中上下导航,而不是像SAX那样是一次性的处理。DOM使用起来也要简单得多。

2)SAX

        SAX处理的优点非常类似于流媒体的优点。分析能够立即开始,而不是等待所有的数据被处理。而且,由于应用程序只是在读取数据时检查数据,因此不需要将数据存储在内存中。这对于大型文档来说是个巨大的优点。事实上,应用程序甚至不必解析整个文档;它可以在某个条件得到满足时停止解析。一般来说,SAX还比它的替代者DOM快许多。 
   选择DOM还是选择SAX? 对于需要自己编写代码来处理XML文档的开发人员来说,  选择DOM还是SAX解析模型是一个非常重要的设计决策。 DOM采用建立树形结构的方式访问XML文档,而SAX采用的事件模型。 

  DOM解析器把XML文档转化为一个包含其内容的树,并可以对树进行遍历。用DOM解析模型的优点是编程容易,开发人员只需要调用建树的指令,然后利用navigation APIs访问所需的树节点来完成任务。可以很容易的添加和修改树中的元素。然而由于使用DOM解析器的时候需要处理整个XML文档,所以对性能和内存的要求比较高,尤其是遇到很大的XML文件的时候。由于它的遍历能力,DOM解析器常用于XML文档需要频繁的改变的服务中。 

  SAX解析器采用了基于事件的模型,它在解析XML文档的时候可以触发一系列的事件,当发现给定的tag的时候,它可以激活一个回调方法,告诉该方法制定的标签已经找到。SAX对内存的要求通常会比较低,因为它让开发人员自己来决定所要处理的tag。特别是当开发人员只需要处理文档中所包含的部分数据时,SAX这种扩展能力得到了更好的体现。但用SAX解析器的时候编码工作会比较困难,而且很难同时访问同一个文档中的多处不同数据。

 3)JDOM           http://www.jdom.org/

          JDOM的目的是成为Java特定文档模型,它简化与XML的交互并且比使用DOM实现更快。由于是第一个Java特定模型,JDOM一直得到大力推广和促进。正在考虑通过“Java规范请求JSR-102”将它最终用作“Java标准扩展”。从2000年初就已经开始了JDOM开发。

  JDOM与DOM主要有两方面不同。首先,JDOM仅使用具体类而不使用接口。这在某些方面简化了API,但是也限制了灵活性。第二,API大量使用了Collections类,简化了那些已经熟悉这些类的Java开发者的使用。

  JDOM文档声明其目的是“使用20%(或更少)的精力解决80%(或更多)Java/XML问题”(根据学习曲线假定为20%)。JDOM对于大多数Java/XML应用程序来说当然是有用的,并且大多数开发者发现API比DOM容易理解得多。JDOM还包括对程序行为的相当广泛检查以防止用户做任何在XML中无意义的事。然而,它仍需要您充分理解XML以便做一些超出基本的工作(或者甚至理解某些情况下的错误)。这也许是比学习DOM或JDOM接口都更有意义的工作。

  JDOM自身不包含解析器。它通常使用SAX2解析器来解析和验证输入XML文档(尽管它还可以将以前构造的DOM表示作为输入)。它包含一些转换器以将JDOM表示输出成SAX2事件流、DOM模型或XML文本文档。JDOM是在Apache许可证变体下发布的开放源码。

4)DOM4J http://dom4j.sourceforge.net/ 
             
        虽然DOM4J代表了完全独立的开发结果,但最初,它是JDOM的一种智能分支。它合并了许多超出基本XML文档表示的功能,包括集成的XPath支持、XML Schema支持以及用于大文档或流化文档的基于事件的处理。它还提供了构建文档表示的选项,它通过DOM4J API和标准DOM接口具有并行访问功能。从2000下半年开始,它就一直处于开发之中。

  为支持所有这些功能,DOM4J使用接口和抽象基本类方法。DOM4J大量使用了API中的Collections类,但是在许多情况下,它还提供一些替代方法以允许更好的性能或更直接的编码方法。直接好处是,虽然DOM4J付出了更复杂的API的代价,但是它提供了比JDOM大得多的灵活性。

  在添加灵活性、XPath集成和对大文档处理的目标时,DOM4J的目标与JDOM是一样的:针对Java开发者的易用性和直观操作。它还致力于成为比JDOM更完整的解决方案,实现在本质上处理所有Java/XML问题的目标。在完成该目标时,它比JDOM更少强调防止不正确的应用程序行为。

  DOM4J是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的Java软件都在使用DOM4J来读写XML,特别值得一提的是连Sun的JAXM也在用DOM4J。 

================================================华丽的分割线=====================================================================

我们在使用freeswitch的时候,在话单保存的时候,由于话单的格式是XML,话单的格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
cdr:<?xml version="1.0"?>
<cdr core-uuid="fbb338f0-4319-41b0-8b15-7347da3ef948" switchname="chen-PC">
  <channel_data>
    <state>CS_REPORTING</state>
    <direction>inbound</direction>
    <state_number>11</state_number>
    <flags>0=1;1=1;3=1;37=1;38=1;40=1;48=1;53=1;74=1;94=1;111=1;112=1;121=1</flags>
    <caps>1=1;2=1;3=1;4=1;5=1;6=1</caps>
  </channel_data>
  <call-stats>
    <audio>
      <inbound>
        <raw_bytes>67424</raw_bytes>
        <media_bytes>67424</media_bytes>
        <packet_count>392</packet_count>
        <media_packet_count>392</media_packet_count>
        <skip_packet_count>6</skip_packet_count>
        <jitter_packet_count>0</jitter_packet_count>
        <dtmf_packet_count>0</dtmf_packet_count>
        <cng_packet_count>0</cng_packet_count>
        <flush_packet_count>0</flush_packet_count>
        <largest_jb_size>0</largest_jb_size>
        <jitter_min_variance>0.00</jitter_min_variance>
        <jitter_max_variance>0.00</jitter_max_variance>
        <jitter_loss_rate>0.00</jitter_loss_rate>
        <jitter_burst_rate>0.00</jitter_burst_rate>
        <mean_interval>20.00</mean_interval>
        <flaw_total>0</flaw_total>
        <quality_percentage>100.00</quality_percentage>
        <mos>4.50</mos>
      </inbound>
      <outbound>
        <raw_bytes>65876</raw_bytes>
        <media_bytes>65876</media_bytes>
        <packet_count>383</packet_count>
        <media_packet_count>383</media_packet_count>
        <skip_packet_count>0</skip_packet_count>
        <dtmf_packet_count>0</dtmf_packet_count>
        <cng_packet_count>0</cng_packet_count>
        <rtcp_packet_count>0</rtcp_packet_count>
        <rtcp_octet_count>0</rtcp_octet_count>
      </outbound>
    </audio>
  </call-stats>
  <variables>
    <direction>inbound</direction>
    <uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e</uuid>
    <session_id>11</session_id>
    <sip_from_user>1005</sip_from_user>
    <sip_from_port>3060</sip_from_port>
    <sip_from_uri>1005%40192.168.2.231%3A3060</sip_from_uri>
    <sip_from_host>192.168.2.231</sip_from_host>
    <video_media_flow>sendrecv</video_media_flow>
    <channel_name>sofia/internal/1005%40192.168.2.231%3A3060</channel_name>
    <sip_call_id>YmQ0MWYxZTQ5NTE5MTJhZTVhODExMmIyMjViZTk0ZDA.</sip_call_id>
    <ep_codec_string>CORE_PCM_MODULE.PCMU%408000h%4020i%4064000b,CORE_PCM_MODULE.PCMA%408000h%4020i%4064000b</ep_codec_string>
    <sip_local_network_addr>36.7.69.235</sip_local_network_addr>
    <sip_network_ip>192.168.2.231</sip_network_ip>
    <sip_network_port>6446</sip_network_port>
    <sip_received_ip>192.168.2.231</sip_received_ip>
    <sip_received_port>6446</sip_received_port>
    <sip_via_protocol>udp</sip_via_protocol>
    <sip_authorized>true</sip_authorized>
    <Event-Name>REQUEST_PARAMS</Event-Name>
    <Core-UUID>fbb338f0-4319-41b0-8b15-7347da3ef948</Core-UUID>
    <FreeSWITCH-Hostname>chen-PC</FreeSWITCH-Hostname>
    <FreeSWITCH-Switchname>chen-PC</FreeSWITCH-Switchname>
    <FreeSWITCH-IPv4>192.168.2.231</FreeSWITCH-IPv4>
    <FreeSWITCH-IPv6>%3A%3A1</FreeSWITCH-IPv6>
    <Event-Date-Local>2017-12-14%2017%3A30%3A31</Event-Date-Local>
    <Event-Date-GMT>Thu,%2014%20Dec%202017%2009%3A30%3A31%20GMT</Event-Date-GMT>
    <Event-Date-Timestamp>1513243831009250</Event-Date-Timestamp>
    <Event-Calling-File>sofia.c</Event-Calling-File>
    <Event-Calling-Function>sofia_handle_sip_i_invite</Event-Calling-Function>
    <Event-Calling-Line-Number>10057</Event-Calling-Line-Number>
    <Event-Sequence>1074</Event-Sequence>
    <sip_number_alias>1005</sip_number_alias>
    <sip_auth_username>1005</sip_auth_username>
    <sip_auth_realm>192.168.2.231</sip_auth_realm>
    <number_alias>1005</number_alias>
    <requested_user_name>1005</requested_user_name>
    <requested_domain_name>172.29.50.6</requested_domain_name>
    <record_stereo>true</record_stereo>
    <default_gateway>example.com</default_gateway>
    <default_areacode>918</default_areacode>
    <transfer_fallback_extension>operator</transfer_fallback_extension>
    <toll_allow>domestic,international,local</toll_allow>
    <accountcode>1005</accountcode>
    <user_context>default</user_context>
    <effective_caller_id_name>Extension%201005</effective_caller_id_name>
    <effective_caller_id_number>1005</effective_caller_id_number>
    <outbound_caller_id_name>FreeSWITCH</outbound_caller_id_name>
    <outbound_caller_id_number>0000000000</outbound_caller_id_number>
    <callgroup>techsupport</callgroup>
    <user_name>1005</user_name>
    <domain_name>172.29.50.6</domain_name>
    <sip_from_user_stripped>1005</sip_from_user_stripped>
    <sip_from_tag>847b4f16</sip_from_tag>
    <sofia_profile_name>internal</sofia_profile_name>
    <recovery_profile_name>internal</recovery_profile_name>
    <sip_full_via>SIP/2.0/UDP%20192.168.2.231%3A6446%3Bbranch%3Dz9hG4bK-d87543-9107f1407d4be545-1--d87543-%3Brport%3D6446</sip_full_via>
    <sip_from_display>1005</sip_from_display>
    <sip_full_from>%221005%22%20%3Csip%3A1005%40192.168.2.231%3A3060%3E%3Btag%3D847b4f16</sip_full_from>
    <sip_to_display>152****0699</sip_to_display>
    <sip_full_to>%22152****0699%22%20%3Csip%3A152****0699%40192.168.2.231%3A3060%3E</sip_full_to>
    <sip_allow>INVITE,%20ACK,%20CANCEL,%20OPTIONS,%20BYE,%20REFER,%20NOTIFY,%20MESSAGE,%20SUBSCRIBE,%20INFO</sip_allow>
    <sip_req_user>152****0699</sip_req_user>
    <sip_req_port>3060</sip_req_port>
    <sip_req_uri>152****0699%40192.168.2.231%3A3060</sip_req_uri>
    <sip_req_host>192.168.2.231</sip_req_host>
    <sip_to_user>152****0699</sip_to_user>
    <sip_to_port>3060</sip_to_port>
    <sip_to_uri>152****0699%40192.168.2.231%3A3060</sip_to_uri>
    <sip_to_host>192.168.2.231</sip_to_host>
    <sip_contact_user>1005</sip_contact_user>
    <sip_contact_port>6446</sip_contact_port>
    <sip_contact_uri>1005%40192.168.2.231%3A6446</sip_contact_uri>
    <sip_contact_host>192.168.2.231</sip_contact_host>
    <sip_user_agent>eyeBeam%20release%201011d%20stamp%2040820</sip_user_agent>
    <sip_via_host>192.168.2.231</sip_via_host>
    <sip_via_port>6446</sip_via_port>
    <sip_via_rport>6446</sip_via_rport>
    <max_forwards>70</max_forwards>
    <presence_id>1005%40192.168.2.231</presence_id>
    <switch_r_sdp>v%3D0%0D%0Ao%3D-%205%202%20IN%20IP4%20192.168.2.231%0D%0As%3DCounterPath%20eyeBeam%201.5%0D%0Ac%3DIN%20IP4%20192.168.2.231%0D%0At%3D0%200%0D%0Am%3Daudio%2022424%20RTP/AVP%200%208%2018%20101%0D%0Aa%3Drtpmap%3A18%20G729/8000%0D%0Aa%3Dfmtp%3A18%20annexb%3Dno%0D%0Aa%3Drtpmap%3A101%20telephone-event/8000%0D%0Aa%3Dfmtp%3A101%200-15%0D%0Aa%3Dalt%3A1%203%20%3A%20zhhREpPi%20r6s0Zvi9%20192.168.2.231%2022424%0D%0Aa%3Dalt%3A2%202%20%3A%20g2vjg3DX%20Ga7%2BbVla%20192.168.18.1%2022424%0D%0Aa%3Dalt%3A3%201%20%3A%20IzxAhjnF%206wdMOPT9%20192.168.199.1%2022424%0D%0Aa%3Dx-rtp-session-id%3A6F56FE94725A4560818F9EA8D51E50DC%0D%0A</switch_r_sdp>
    <DP_MATCH>152****0699</DP_MATCH>
    <DP_MATCH>152****0699</DP_MATCH>
    <call_uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e</call_uuid>
    <hangup_after_bridge>true</hangup_after_bridge>
    <my_call_uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</my_call_uuid>
    <current_application_data>%7Borigination_uuid%3D6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg,absolute_codec_string%3Dpcma,origination_caller_id_number%3D65222895,origination_caller_id_name%3D65222895,effective_caller_id_number%3D65222895,effective_caller_id_name%3D65222895%7Dsofia/gateway/MRWG/152****0699</current_application_data>
    <current_application>bridge</current_application>
    <originated_legs>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg%3BOutbound%20Call%3B152****0699</originated_legs>
    <switch_m_sdp>v%3D0%0D%0Ao%3DFreeSWITCH%201513195822%201513195823%20IN%20IP4%2036.33.216.174%0D%0As%3DFreeSWITCH%0D%0Ac%3DIN%20IP4%2036.33.216.174%0D%0At%3D0%200%0D%0Am%3Daudio%2048016%20RTP/AVP%208%20101%0D%0Aa%3Drtpmap%3A8%20PCMA/8000%0D%0Aa%3Drtpmap%3A101%20telephone-event/8000%0D%0Aa%3Dfmtp%3A101%200-16%0D%0Aa%3Dptime%3A20%0D%0A</switch_m_sdp>
    <audio_media_flow>sendrecv</audio_media_flow>
    <rtp_use_codec_string>ilbc,OPUS,G722,PCMU,PCMA,VP8</rtp_use_codec_string>
    <rtp_audio_recv_pt>0</rtp_audio_recv_pt>
    <rtp_use_codec_name>PCMU</rtp_use_codec_name>
    <rtp_use_codec_rate>8000</rtp_use_codec_rate>
    <rtp_use_codec_ptime>20</rtp_use_codec_ptime>
    <rtp_use_codec_channels>1</rtp_use_codec_channels>
    <rtp_last_audio_codec_string>PCMU%408000h%4020i%401c</rtp_last_audio_codec_string>
    <read_codec>PCMU</read_codec>
    <original_read_codec>PCMU</original_read_codec>
    <read_rate>8000</read_rate>
    <original_read_rate>8000</original_read_rate>
    <write_codec>PCMU</write_codec>
    <write_rate>8000</write_rate>
    <dtmf_type>rfc2833</dtmf_type>
    <local_media_ip>192.168.2.231</local_media_ip>
    <local_media_port>20114</local_media_port>
    <advertised_media_ip>192.168.2.231</advertised_media_ip>
    <rtp_use_timer_name>soft</rtp_use_timer_name>
    <rtp_use_pt>0</rtp_use_pt>
    <rtp_use_ssrc>1605568662</rtp_use_ssrc>
    <rtp_2833_send_payload>101</rtp_2833_send_payload>
    <rtp_2833_recv_payload>101</rtp_2833_recv_payload>
    <remote_media_ip>192.168.2.231</remote_media_ip>
    <remote_media_port>22424</remote_media_port>
    <originate_disposition>SUCCESS</originate_disposition>
    <DIALSTATUS>SUCCESS</DIALSTATUS>
    <originate_causes>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg%3BNONE</originate_causes>
    <last_bridge_to>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</last_bridge_to>
    <bridge_channel>sofia/external/152****0699</bridge_channel>
    <bridge_uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</bridge_uuid>
    <signal_bond>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</signal_bond>
    <rtp_local_sdp_str>v%3D0%0D%0Ao%3DFreeSWITCH%201513223724%201513223726%20IN%20IP4%20192.168.2.231%0D%0As%3DFreeSWITCH%0D%0Ac%3DIN%20IP4%20192.168.2.231%0D%0At%3D0%200%0D%0Am%3Daudio%2020114%20RTP/AVP%200%20101%0D%0Aa%3Drtpmap%3A0%20PCMU/8000%0D%0Aa%3Drtpmap%3A101%20telephone-event/8000%0D%0Aa%3Dfmtp%3A101%200-16%0D%0Aa%3Dptime%3A20%0D%0Aa%3Dsendrecv%0D%0A</rtp_local_sdp_str>
    <endpoint_disposition>ANSWER</endpoint_disposition>
    <sip_reason>Q.850%3Bcause%3D16%3Btext%3D%22NORMAL_CLEARING%22</sip_reason>
    <sip_hangup_phrase>OK</sip_hangup_phrase>
    <last_bridge_hangup_cause>NORMAL_CLEARING</last_bridge_hangup_cause>
    <last_bridge_proto_specific_hangup_cause>sip%3A200</last_bridge_proto_specific_hangup_cause>
    <bridge_hangup_cause>NORMAL_CLEARING</bridge_hangup_cause>
    <hangup_cause>NORMAL_CLEARING</hangup_cause>
    <hangup_cause_q850>16</hangup_cause_q850>
    <digits_dialed>none</digits_dialed>
    <start_stamp>2017-12-14%2017%3A30%3A31</start_stamp>
    <profile_start_stamp>2017-12-14%2017%3A30%3A31</profile_start_stamp>
    <answer_stamp>2017-12-14%2017%3A30%3A43</answer_stamp>
    <bridge_stamp>2017-12-14%2017%3A30%3A38</bridge_stamp>
    <progress_media_stamp>2017-12-14%2017%3A30%3A38</progress_media_stamp>
    <end_stamp>2017-12-14%2017%3A30%3A46</end_stamp>
    <start_epoch>1513243831</start_epoch>
    <start_uepoch>1513243831009250</start_uepoch>
    <profile_start_epoch>1513243831</profile_start_epoch>
    <profile_start_uepoch>1513243831009250</profile_start_uepoch>
    <answer_epoch>1513243843</answer_epoch>
    <answer_uepoch>1513243843549250</answer_uepoch>
    <bridge_epoch>1513243838</bridge_epoch>
    <bridge_uepoch>1513243838979250</bridge_uepoch>
    <last_hold_epoch>0</last_hold_epoch>
    <last_hold_uepoch>0</last_hold_uepoch>
    <hold_accum_seconds>0</hold_accum_seconds>
    <hold_accum_usec>0</hold_accum_usec>
    <hold_accum_ms>0</hold_accum_ms>
    <resurrect_epoch>0</resurrect_epoch>
    <resurrect_uepoch>0</resurrect_uepoch>
    <progress_epoch>0</progress_epoch>
    <progress_uepoch>0</progress_uepoch>
    <progress_media_epoch>1513243838</progress_media_epoch>
    <progress_media_uepoch>1513243838979250</progress_media_uepoch>
    <end_epoch>1513243846</end_epoch>
    <end_uepoch>1513243846889250</end_uepoch>
    <last_app>bridge</last_app>
    <last_arg>%7Borigination_uuid%3D6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg,absolute_codec_string%3Dpcma,origination_caller_id_number%3D65222895,origination_caller_id_name%3D65222895,effective_caller_id_number%3D65222895,effective_caller_id_name%3D65222895%7Dsofia/gateway/MRWG/152****0699</last_arg>
    <caller_id>%221005%22%20%3C1005%3E</caller_id>
    <duration>15</duration>
    <billsec>3</billsec>
    <progresssec>0</progresssec>
    <answersec>12</answersec>
    <waitsec>7</waitsec>
    <progress_mediasec>7</progress_mediasec>
    <flow_billsec>15</flow_billsec>
    <mduration>15880</mduration>
    <billmsec>3340</billmsec>
    <progressmsec>0</progressmsec>
    <answermsec>12540</answermsec>
    <waitmsec>7970</waitmsec>
    <progress_mediamsec>7970</progress_mediamsec>
    <flow_billmsec>15880</flow_billmsec>
    <uduration>15880000</uduration>
    <billusec>3340000</billusec>
    <progressusec>0</progressusec>
    <answerusec>12540000</answerusec>
    <waitusec>7970000</waitusec>
    <progress_mediausec>7970000</progress_mediausec>
    <flow_billusec>15880000</flow_billusec>
    <sip_hangup_disposition>send_bye</sip_hangup_disposition>
    <rtp_audio_in_raw_bytes>67424</rtp_audio_in_raw_bytes>
    <rtp_audio_in_media_bytes>67424</rtp_audio_in_media_bytes>
    <rtp_audio_in_packet_count>392</rtp_audio_in_packet_count>
    <rtp_audio_in_media_packet_count>392</rtp_audio_in_media_packet_count>
    <rtp_audio_in_skip_packet_count>6</rtp_audio_in_skip_packet_count>
    <rtp_audio_in_jitter_packet_count>0</rtp_audio_in_jitter_packet_count>
    <rtp_audio_in_dtmf_packet_count>0</rtp_audio_in_dtmf_packet_count>
    <rtp_audio_in_cng_packet_count>0</rtp_audio_in_cng_packet_count>
    <rtp_audio_in_flush_packet_count>0</rtp_audio_in_flush_packet_count>
    <rtp_audio_in_largest_jb_size>0</rtp_audio_in_largest_jb_size>
    <rtp_audio_in_jitter_min_variance>0.00</rtp_audio_in_jitter_min_variance>
    <rtp_audio_in_jitter_max_variance>0.00</rtp_audio_in_jitter_max_variance>
    <rtp_audio_in_jitter_loss_rate>0.00</rtp_audio_in_jitter_loss_rate>
    <rtp_audio_in_jitter_burst_rate>0.00</rtp_audio_in_jitter_burst_rate>
    <rtp_audio_in_mean_interval>20.00</rtp_audio_in_mean_interval>
    <rtp_audio_in_flaw_total>0</rtp_audio_in_flaw_total>
    <rtp_audio_in_quality_percentage>100.00</rtp_audio_in_quality_percentage>
    <rtp_audio_in_mos>4.50</rtp_audio_in_mos>
    <rtp_audio_out_raw_bytes>65876</rtp_audio_out_raw_bytes>
    <rtp_audio_out_media_bytes>65876</rtp_audio_out_media_bytes>
    <rtp_audio_out_packet_count>383</rtp_audio_out_packet_count>
    <rtp_audio_out_media_packet_count>383</rtp_audio_out_media_packet_count>
    <rtp_audio_out_skip_packet_count>0</rtp_audio_out_skip_packet_count>
    <rtp_audio_out_dtmf_packet_count>0</rtp_audio_out_dtmf_packet_count>
    <rtp_audio_out_cng_packet_count>0</rtp_audio_out_cng_packet_count>
    <rtp_audio_rtcp_packet_count>0</rtp_audio_rtcp_packet_count>
    <rtp_audio_rtcp_octet_count>0</rtp_audio_rtcp_octet_count>
  </variables>
  <app_log>
    <application app_name="set" app_data="hangup_after_bridge=true" app_stamp="1513243831019750"></application>
    <application app_name="set" app_data="my_call_uuid=6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg" app_stamp="1513243831019750"></application>
    <application app_name="log" app_data="INFO my_call_uuid is: iLBC...iLBC...iLBC...iLBC...  1005 , myPrefix is:  , ON: " app_stamp="1513243831019750"></application>
    <application app_name="bridge" app_data="{origination_uuid=6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg,absolute_codec_string=pcma,origination_caller_id_number=65222895,origination_caller_id_name=65222895,effective_caller_id_number=65222895,effective_caller_id_name=65222895}sofia/gateway/MRWG/152****0699" app_stamp="1513243831020250"></application>
  </app_log>
  <callflow dialplan="XML" unique-id="d7ea3c30-cc0d-4e48-b3cf-09ce3ae70332" profile_index="1">
    <extension name="call mobile extension" number="152****0699" current_app="hangup">
      <application app_name="set" app_data="hangup_after_bridge=true"></application>
      <application app_name="set" app_data="my_call_uuid=${uuid}-bleg"></application>
      <application app_name="log" app_data="INFO my_call_uuid is: iLBC...iLBC...iLBC...iLBC...  ${caller_id_number} , myPrefix is: ${checkmobile} , ON: ${outboundNumber}"></application>
      <application app_name="bridge" app_data="{origination_uuid=${my_call_uuid},absolute_codec_string=pcma,origination_caller_id_number=65222895,origination_caller_id_name=65222895,effective_caller_id_number=65222895,effective_caller_id_name=65222895}sofia/gateway/MRWG/152****0699"></application>
      <application last_executed="true" app_name="hangup" app_data=""></application>
    </extension>
    <caller_profile>
      <username>1005</username>
      <dialplan>XML</dialplan>
      <caller_id_name>1005</caller_id_name>
      <caller_id_number>1005</caller_id_number>
      <callee_id_name>Outbound Call</callee_id_name>
      <callee_id_number>152****0699</callee_id_number>
      <ani>1005</ani>
      <aniii></aniii>
      <network_addr>192.168.2.231</network_addr>
      <rdnis></rdnis>
      <destination_number>152****0699</destination_number>
      <uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e</uuid>
      <source>mod_sofia</source>
      <context>default</context>
      <chan_name>sofia/internal/1005@192.168.2.231:3060</chan_name>
      <origination>
        <origination_caller_profile>
          <username>1005</username>
          <dialplan>XML</dialplan>
          <caller_id_name>65222895</caller_id_name>
          <caller_id_number>65222895</caller_id_number>
          <callee_id_name>Outbound Call</callee_id_name>
          <callee_id_number>152****0699</callee_id_number>
          <ani>1005</ani>
          <aniii></aniii>
          <network_addr>192.168.2.231</network_addr>
          <rdnis></rdnis>
          <destination_number>152****0699</destination_number>
          <uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</uuid>
          <source>mod_sofia</source>
          <context>default</context>
          <chan_name>sofia/external/152****0699</chan_name>
        </origination_caller_profile>
      </origination>
      <originatee>
        <originatee_caller_profile>
          <username>1005</username>
          <dialplan>XML</dialplan>
          <caller_id_name>65222895</caller_id_name>
          <caller_id_number>65222895</caller_id_number>
          <callee_id_name>Outbound Call</callee_id_name>
          <callee_id_number>152****0699</callee_id_number>
          <ani>1005</ani>
          <aniii></aniii>
          <network_addr>36.33.216.174</network_addr>
          <rdnis></rdnis>
          <destination_number>152****0699</destination_number>
          <uuid>6b6e935c-fb2d-4af5-9d9e-c48ff512e37e-bleg</uuid>
          <source>mod_sofia</source>
          <context>default</context>
          <chan_name>sofia/external/152****0699</chan_name>
        </originatee_caller_profile>
      </originatee>
    </caller_profile>
    <times>
      <created_time>1513243831009250</created_time>
      <profile_created_time>1513243831009250</profile_created_time>
      <progress_time>0</progress_time>
      <progress_media_time>1513243838979250</progress_media_time>
      <answered_time>1513243843549250</answered_time>
      <bridged_time>1513243838979250</bridged_time>
      <last_hold_time>0</last_hold_time>
      <hold_accum_time>0</hold_accum_time>
      <hangup_time>1513243846889250</hangup_time>
      <resurrect_time>0</resurrect_time>
      <transfer_time>0</transfer_time>
    </times>
  </callflow>
</cdr>

  为了得到我们需要的字段,包括主叫,被叫,通话时间,接通时间,应答时间等信息。我们从XML格式的文件中取出相应的字段值。

后台处理如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***
     * 解析xml话单
     ***/
    public void analysis(String fileContent) throws Exception {
        SAXReader reader = new SAXReader();
        InputStream input = new ByteArrayInputStream(fileContent.getBytes());
        Document document = reader.read(input);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();
        for (Element child : childElements) {
            System.out.println(child.getName()+"===============");
            if (child.getName() == "variables") {
                List<Element> varElements = child.elements();
                for (Element var : varElements) {
                    String elementName = var.getName().toLowerCase().trim();
                    switch (elementName) {
                    case "uuid":
                        uuid = var.getTextTrim();
                        break;
                    case "sip_auth_username":
                        sip_auth_username = var.getTextTrim();
                        break;
                         
                    // 自定义变量   
                    case "my_opnum_name":
                        my_opnum_name = var.getTextTrim();
                        break;
                    case "my_extnum_name":
                        my_extnum_name = var.getTextTrim();
                        break;
                    case "my_caller_name":
                        my_caller_name = var.getTextTrim();
                        break;
                    case "my_callee_name":
                        my_callee_name = var.getTextTrim();
                        break;
                    case "my_answer_stamp":
                        my_answer_stamp = var.getTextTrim();
                        break;
                         
                    case "start_stamp":
                        start_stamp = var.getTextTrim();
                        break;
                    case "answer_stamp":
                        answer_stamp = var.getTextTrim();
                        break;
                    case "end_stamp":
                        end_stamp = var.getTextTrim();
                        break;
                    case "auto_batchcall_flag":
                        auto_batchcall_flag = var.getTextTrim();
                        break;
                    case "not_save_record_flag":
                        not_save_record_flag = var.getTextTrim();
                        break;
                    case "callspy_flag":
                        callspy_flag = var.getTextTrim();
                        break;
                    case "last_bridge_hangup_cause":
                        hangup_cause += var.getTextTrim();
                        break;
                    }
                }
            }
 
            if (child.getName() == "callflow") {
                List<Element> varElements = child.elements();
                for (Element varEle : varElements) {
                    if (varEle.getName() == "caller_profile") {
                        List<Element> varElementsSub = varEle.elements();
                        for (Element var : varElementsSub) {
                            String elementName = var.getName().toLowerCase().trim();
                            switch (elementName) {
                            case "caller_id_number":
                                caller_id_number = var.getTextTrim();
                                break;
                            case "destination_number":
                                destination_number = var.getTextTrim();
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

 解析后将得到的值赋到相应的变量中。

 

==========================下面是一个简单的测试=================

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
 
public class TestCdrXml {
    @Test
    public void test1() throws DocumentException{
        String cdrxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
                +"<student>"
                +"<name>张三</name>"
                + "<age>18</age>"
                + "</student>";
        SAXReader reader = new SAXReader();
        InputStream input = new ByteArrayInputStream(cdrxml.getBytes());
        Document document = reader.read(input);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();
        for(Element ele:childElements){
            System.out.println(ele.getName()+":"+ele.getTextTrim());
        }
    }
}

  运行后结果如下:

name:张三
age:18

 

posted @   Bob_C  阅读(748)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
点击右上角即可分享
微信分享提示