上一章讲到了将XML数据从DB2数据库中读出来,并转换成org.w3c.dom.Document对象。今天这一章讲述一下,利用JAVA反射机制将org.w3c.dom.Document对象Element中的内容放进其对应得实体类对象中。这里需要注意的是,xml对象中的标签内容要与实体类对象中的各个属性一一对应。
1
H0011013
0
Y
M
0
2009011875
<span style="font-family: Arial, Helvetica, sans-serif;">比如xml对象中的标签格式为上述所示。那么一定要有对应的实体类。对应的实体类中至少要包括所有标签值对应的成员变量。实体类如下</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span>
<div>package com;
import java.io.Serializable;
public class KeyInfoVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5050931130132593173L;
private String coyNum = null ;
private String plcyNum = null ;
private String autoUWResult = null ;
private String hasBeenCharged = null ;
private String prospectingMode = null ;
private String printStyle = null ;
private String confirmationNO = null ;
public String getCoyNum() {
return coyNum;
}
public void setCoyNum(String coyNum) {
this.coyNum = coyNum;
}
public String getPlcyNum() {
return plcyNum;
}
public void setPlcyNum(String plcyNum) {
this.plcyNum = plcyNum;
}
public String getAutoUWResult() {
return autoUWResult;
}
public void setAutoUWResult(String autoUWResult) {
this.autoUWResult = autoUWResult;
}
public String getHasBeenCharged() {
return hasBeenCharged;
}
public void setHasBeenCharged(String hasBeenCharged) {
this.hasBeenCharged = hasBeenCharged;
}
public String getProspectingMode() {
return prospectingMode;
}
public void setProspectingMode(String prospectingMode) {
this.prospectingMode = prospectingMode;
}
public String getPrintStyle() {
return printStyle;
}
public void setPrintStyle(String printStyle) {
this.printStyle = printStyle;
}
public String getConfirmationNO() {
return confirmationNO;
}
public void setConfirmationNO(String confirmationNO) {
this.confirmationNO = confirmationNO;
}
}
</div>
测试代码如下:
<pre name="code" class="html"><div>package com;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlConverter {
public static void main(String[] args) {
//org.w3c.dom.Document w3doc = null;
//w3doc = Dom4jToW3CDoc.toW3CDocument(blob);
File file = new File("D:\\123.xml");
try {
FileInputStream fis = new FileInputStream(file);
SAXReader reader = new SAXReader();
org.dom4j.Document doc = null ;
org.w3c.dom.Document w3doc = null;
org.dom4j.io.DOMWriter d4Writer = new org.dom4j.io.DOMWriter();
doc = reader.read(fis) ; //将输入流转换成org.dom4j.Document
doc.setXMLEncoding("GBK"); //设置编码格式
w3doc = d4Writer.write(doc); //org.dom4j.Document转换成org.w3c.dom.Document
Object o = queryKeyInfo(w3doc);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Object queryKeyInfo(Document doc) {
final KeyInfoVO info = new KeyInfoVO();
setObject(doc, "KeyInfo", info);
return info;
}
public static Object setObject(Document doc, String inTagName,
final Object obj) {
try{
//得到文档名称为inTagName的元素的节点列表 (这里为KeyInfoVO)
NodeList nodeList = doc.getElementsByTagName(inTagName);
for (int i = 0; i < nodeList.getLength(); i++) {
//获得KeyInfoVO标签元素
Element e = (Element) nodeList.item(i);
//获得KeyInfoVO标签下的所有子元素
NodeList childNodes = e.getChildNodes();
//遍历KeyInfoVO标签下的所有子元素
for (int j = 0; j < childNodes.getLength(); j++) {
Node p_detail = childNodes.item(j);
//判断当前节点是否为单节点元素
if (p_detail instanceof Element) {
Element e1 = (Element) p_detail;
String tagName = e1.getTagName();
String textContent = e1
.getTextContent();
setValue(obj, tagName, textContent);
}
}
}
return obj;
}catch(Exception e){
throw new RuntimeException("解析查询报文出错",e);
}
}
/**
* 设置值
* */
private static void setValue(Object obj, String name, String value) {
String firstWord = (name.charAt(0) + "").toUpperCase();
name = firstWord + name.substring(1, name.length());
try {
Class[] cs = { String.class };
Method m = obj.getClass().getDeclaredMethod("set" + name, cs);
m.invoke(obj, value);
} catch (Exception e) {
}
}
}
</div>