CollTuxedoXmlHelper.java
5.79 KB
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
package com.sitech.ismp.coll.tivoli;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class CollTuxedoXmlHelper {
public static List parseCommonSoapResponse(String content)
{
List list_tmp = new java.util.Vector();
java.util.Map map_tmp = null;
try{
String str_reponse = content;
Document doc_response = XMLUtil.parseDocumentFromString(str_reponse);
NodeList nl = doc_response.getElementsByTagName("ROW");
if(nl.getLength()==0){
//throw new SoapAccessException("----------no data response!!!----------");
}
for ( int i=0; i<nl.getLength(); i++ ){
Element rowElement = (Element) nl.item(i);
map_tmp = new HashMap();
//得到所有的有key-value的node
NodeList vlaueNodes = rowElement.getChildNodes();
for(int j=0; j<vlaueNodes.getLength(); j++)
{
Node valueNode = vlaueNodes.item(j);
if("#text".equals(valueNode.getNodeName())){
continue;
}
String s_key = valueNode.getNodeName();
String s_value = "";
Node first = valueNode.getFirstChild();
if(first == null){
s_value="";
}else{
s_value = first.getNodeValue();
}
map_tmp.put( s_key, s_value.trim() ) ;
//System.out.println(s_key+":"+s_value);
}
list_tmp.add(map_tmp);
}
}catch(Exception e){
e.printStackTrace();
}
return list_tmp;
}
/**
* 分析参数类型返回值, 即每行一个指标
*
* 我们统一认为有三项
* 1 采集指标
* 2 采集值
* 3 采集时间
*
* 例如:
// <ROW>
// <BBPARMS> MAXSERVERS</BBPARMS>
// <MetricValue dt="number">2000</MetricValue>
// <LocalTimeStamp>1060628143502000</LocalTimeStamp>
// </ROW>
// <ROW>
// <BBPARMS> MAXACCESSERS</BBPARMS>
// <MetricValue dt="number">4000</MetricValue>
// <LocalTimeStamp>1060628143502020</LocalTimeStamp>
// </ROW>
*
* @param content
* @param content
* @param content
* @return 返回一个List, List中各项代表一个三元组[kpi, value ,cll_time ].List各项为String[],顺序为kpi,value ,cll_time
*/
public static List parseParamsSoapResponse(String content, String param_node, String value_node,String time_node )
{
List list_tmp = new java.util.Vector();
try{
String str_reponse = content;
Document doc_response = XMLUtil.parseDocumentFromString(str_reponse);
NodeList nl = doc_response.getElementsByTagName("ROW");
if(nl.getLength()==0){
//throw new SoapAccessException("----------no data response!!!----------");
}
//每个代表一个row
for ( int i=0; i<nl.getLength(); i++ ){
Element rowElement = (Element) nl.item(i);
String[] kpidetail = new String[3]; //0:kpi_name , 1:kpi_value , 2 cll_time
kpidetail[0]=null;
kpidetail[1]=null;
kpidetail[2]=null;
//得到所有的有key-value的node
NodeList vlaueNodes = rowElement.getChildNodes();
for(int j=0; j<vlaueNodes.getLength(); j++)
{
Node valueNode = vlaueNodes.item(j);
if("#text".equals(valueNode.getNodeName())){
continue;
}
String s_key = valueNode.getNodeName();
if(s_key.equals(param_node)){
//kpi 名称
String s_value = "";
Node first = valueNode.getFirstChild();
if(first == null){
s_value="";
}else{
s_value = first.getNodeValue();
}
kpidetail[0]=s_value.trim();
}else if(s_key.equals(value_node)){
//采集值
String s_value = "";
Node first = valueNode.getFirstChild();
if(first == null){
s_value="";
}else{
s_value = first.getNodeValue();
}
kpidetail[1]=s_value.trim();
}else if(s_key.equals(time_node)){
//采集时间
String s_value = "";
Node first = valueNode.getFirstChild();
if(first == null){
s_value="";
}else{
s_value = first.getNodeValue();
}
kpidetail[2]=s_value.trim();
}else{
continue;
}
//System.out.println(s_key+":"+s_value);
}
list_tmp.add(kpidetail);
}
}catch(Exception e){
e.printStackTrace();
}
return list_tmp;
}
public static void main(String[] args)throws Exception{
String kpi_node="BBPARMS";
String value_node="MetricValue";
String time_node="LocalTimeStamp";
String content="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
" <DATA><ROW>"+
" <BBPARMS>MAXSERVERS</BBPARMS> "+
" <MetricValue dt=\"number\">2000</MetricValue>"+
" <LocalTimeStamp>1060628143502000</LocalTimeStamp>"+
" </ROW> "+
" <ROW> "+
" <BBPARMS>MAXACCESSERS</BBPARMS> "+
" <MetricValue dt=\"number\">4000</MetricValue> "+
" <LocalTimeStamp>1060628143502020</LocalTimeStamp> "+
" </ROW></DATA> ";
System.out.println(content);
List results = CollTuxedoXmlHelper.parseParamsSoapResponse(content,kpi_node,value_node,time_node);
for(int i=0; i<results.size(); i++){
String[] kpidetail = (String[])results.get(i);
System.out.print(kpidetail[0]+" : ");
System.out.print(kpidetail[1]+" : ");
System.out.println(kpidetail[2]);
}
}
}