日期:2014-05-16  浏览次数:20467 次

java操作excel实现从数据库导入导出(三)
这篇文章继续讲关于java操作excel实现数据库导入导出,这篇涉及关于工具类的设计与,数据工具类的涉及,第一篇文章我有提起,关于这个操作我们可以使用多种方式去描述,第一properties这个格式描述,或者另外一种形式使用xml形式去描述!在这个设计中我也做了一些实验,对于xml的解析,我们这边主要采用DOM4J,我们主要导入DOM4J-1.6.1.jar,xstream-1.3.1.jar还有一个javaxen-1.1-beta-6.jar。
package xls.util;

import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXValidator;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author panming
 * @since 2008-5-6
 */
public final class Dom4jUtil {

	/**
     * 取锟斤拷Root锟节碉拷
     * @param document
     * @return
     */
	public static Element getRootElement(Document document) {
        return document.getRootElement();
    }
	
	/**
     * 锟斤拷锟斤拷锟街凤拷芨锟绞斤拷锟絰ml锟斤拷锟斤拷
     * @param document
     * @return
     */
	public String getStringDocument(Document document) {
		 String xmlContent = null;
		 if(document != null){
			 xmlContent = document.asXML();
		 }
		 return xmlContent;   
	}   

    /**
	 * 锟斤拷锟截节碉拷路锟斤拷
	 * @param rootNodeName(锟斤拷诘锟斤拷锟斤拷)
	 * @param childNodeName(锟接节碉拷锟斤拷锟?
	 * @return String(锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟?
	 */
	public static String getNodePath(String rootNodeName, String childNodeName) {
		String result = null;
		if (rootNodeName == "" || rootNodeName == null || childNodeName == ""
				|| childNodeName == null) {
			return result;
		}
		result = getNodePath(rootNodeName, childNodeName, null);
		return result;
	}

	/**
	 * 锟斤拷锟截节碉拷路锟斤拷
	 * @param rootNodeName (锟斤拷诘锟斤拷锟斤拷)
	 * @param childNodeName (锟接节碉拷锟斤拷锟?
	 * @param childAttributeName (锟斤拷锟斤拷锟斤拷锟?
	 * @return String (锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟?
	 */
	public static String getNodePath(String rootNodeName, String childNodeName,
			String childAttributeName) {
		String result = null;
		if (rootNodeName == "" || rootNodeName == null || childNodeName == ""
				|| childNodeName == null) {
			return result;
		}
		String dima = "/";
		StringBuffer nodePath = new StringBuffer();
		nodePath.append(rootNodeName);
		nodePath.append(dima).append(childNodeName);
		if (childAttributeName != null && childAttributeName != "") {
			nodePath.append(dima).append("@");
			nodePath.append(childAttributeName);
		}
		result = nodePath.toString();
		return result;
	}
	
    /**
	 * 取锟斤拷Document锟斤拷指锟斤拷锟节碉拷锟铰碉拷锟斤拷锟叫节点集锟斤拷
	 * @param document (document锟斤拷锟斤拷)
	 * @param nodeName (锟斤拷式锟斤拷锟矫碉拷xPath锟斤拷锟? "/books/book/@show"
	 * @return List
	 */
    public static List<?> getListByDocument(Document document, String nodeName) {
		if (document == null || nodeName == null || nodeName == "") {
			return null;
		}
		List<?> list = null;
		list = document.selectNodes(nodeName);
		return list;
	}
    
	/**
	 * 取锟矫节点集锟斤拷 
	 * @param filePathAndName(xml锟侥硷拷)
	 * @param nodePath(锟节碉拷xpath锟斤拷锟?
	 * @return List(锟节碉拷锟叫憋拷)
	 * @throws DocumentException
	 */
	public static List<?> getNodeList(String filePathAndName, String nodePath)
			throws DocumentException {
		List<?> list = null;
		if (filePathAndName == null || filePathAndName == ""
				|| nodePath == null || nodePath == "") {
			return list;
		}
		list = getListByDocume