1. 程式人生 > >(三)JAVA利用反射機制將XML中的內容放入實體物件中

(三)JAVA利用反射機制將XML中的內容放入實體物件中

       上一章講到了將XML資料從DB2資料庫中讀出來,並轉換成org.w3c.dom.Document物件今天這一章講述一下,利用JAVA反射機制將org.w3c.dom.Document物件Element中的內容放進其對應得實體類物件中。這裡需要注意的是,xml物件中的標籤內容要與實體類物件中的各個屬性一一對應。

<?xml version="1.0" encoding="UTF-8"?>
1H00110130YM02009011875
<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>