?? svccontent.java
字號:
package mcm.adc;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class SvcContent {
/** 分析結果 */
protected Document dom;
/**
* 把報文分析成一個Document
*
* @param svcContent
* @return
*/
public SvcContent(String svcContent) {
try {
svcContent = "<?xml version=\"1.0\"?>\n" + svcContent;
InputSource in = new InputSource(new ByteArrayInputStream(svcContent.getBytes("UTF-8")));
DOMParser parser = new DOMParser();
parser.parse(in);
dom = parser.getDocument();
Field fldList[] = this.getClass().getDeclaredFields();
for (int i = 0; i < fldList.length; i++) {
Field fld = fldList[i];
fld.setAccessible(true);
try {
String fldName = fld.getName();
String value = getFirstNodeText(fldName);
if (value != null) {
if (fld.getType().equals(int.class)) {
fld.setInt(this, Integer.parseInt(value));
} else {
fld.set(this, value);
}
}
} catch (Exception ex) {
// 無視
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* 取得Document中出現的第一個TagName中的文字信息
*
* @param dom
* @param tagName
* @return
*/
protected String getFirstNodeText(String tagName) {
NodeList list = dom.getElementsByTagName(tagName);
if (list.getLength() > 0) {
return list.item(0).getTextContent();
}
return null;
}
/**
* 根據類結構,取得數據
*
* @param node
* @param dataClass
* @return
*/
protected Object getDataObject(Node node, Class dataClass) {
try {
Object data = dataClass.newInstance();
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++){
Node subNode = list.item(i);
try{
Field fld = dataClass.getDeclaredField(subNode.getNodeName());
String textContent = subNode.getTextContent();
fld.setAccessible(true);
if (fld.getType().equals(int.class)) {
fld.setInt(data, Integer.parseInt(textContent));
} else {
fld.set(data, textContent);
}
}
catch(Exception ex){
// 無視
}
}
return data;
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -