转载地址:http://yjck.iteye.com/blog/2078953
在此记录一个xml的工具类,该类可以读取xml文件,存入磁盘,并将xml转换为实体对象。
package com.sgcc.ahepc.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XMLUtil { public static Document parse(String path) throws DocumentException{ SAXReader reader = new SAXReader(); return reader.read(new File(path)); } public static Document parse(InputStream in) throws DocumentException{ SAXReader reader = new SAXReader(); return reader.read(in); } public static Document parse(URL url) throws DocumentException{ SAXReader reader = new SAXReader(); return reader.read(url); } /** * X-path的查找方式 * @param document */ @SuppressWarnings("unchecked") public static List<Element> xpathFind(Document document,String nodes){ List<Element> list = document.selectNodes(nodes); return list; } public static String writeXMLToDist(Document document,String basePath,String fileName) throws IOException { File file = new File(basePath+fileName); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); //XMLWriter writer = new XMLWriter(new FileWriter(file),format); XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); writer.write(document); writer.close(); return basePath+fileName; } /** * 该方法能返回单对象Bean * @param document * @param xpath 路径“/aaa/bbb” * @param obj 传入的实体对象 * @return 返回该实体对象,使用时强转 * @throws Exception */ @SuppressWarnings("unchecked") public static Object convertXMLToBean(Document document,String xpath,Object obj) throws Exception { String basePath = xpath; Class c = obj.getClass(); Field[] fields = c.getDeclaredFields(); for(int i=0;i<fields.length;i++){ String name = fields[i].getName(); String methodName = "set"+name.substring(0,1).toUpperCase()+name.substring(1); Node node = document.selectSingleNode(basePath+"/"+name.toUpperCase()); if(node == null){ continue; } Method method = c.getDeclaredMethod(methodName,String.class); method.invoke(obj,node.getText()); } return obj; } /** * 用于变量计划型 注意传入的实体类需要提供getBean的方法,返回一个新实体 * @param document * @param xpath * @param obj * @return * @throws Exception */ @SuppressWarnings("unchecked") public static Object convertXMLToBeans(Document document,Object obj) throws Exception { List<Object> lists = new ArrayList<Object>(); List list = document.selectNodes(xpath); for (int i = 0; i < list.size(); i++) { Element elements = (Element) list.get(i); for (Iterator<Element> j = elements.elementIterator(); j.hasNext();) { Element element = j.next(); Class d = obj.getClass(); Field[] dfields = d.getDeclaredFields(); Method instance = d.getMethod("getBean"); Object newObj = instance.invoke(obj); for (int k = 0; k < dfields.length; k++) { String name = dfields[k].getName(); String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1); Node node = element.selectSingleNode(name.toUpperCase()); if (node == null) { continue; } Method method = d.getDeclaredMethod(methodName,String.class); method.invoke(newObj,node.getText()); } lists.add(newObj); } } return lists; } }该类支持无属性的xml转换为实体bean,节点的名称和实体bean的名称一致,多个bean的的xml需要在实体类中提供getBean()的方法,整体采用java反射的机制进行,还比较实用。