📄 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 + -