java 用dom4j把一個List T物件生成xml案例
阿新 • • 發佈:2019-01-09
dom4j準備
jar:dom4j-1.6.1.jar,該jar主要是生成和解析xml用
fastjson-1.2.49.jar,該jar是alibaba的,主要是用來把List<T>物件轉換成json
dom4j生成xml工具類(泛型)
package com.xm.xml.test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * dom4j工具類 * @author ouyangjun */ public class Dom4jUtils { /** * dom4j xml生成方法 * @param file * @param list * @param clz */ @SuppressWarnings("unchecked") public static <T> void createXml(File file, List<T> list, Class<T> clz){ try{ // 建立Document Document document = DocumentHelper.createDocument(); // 建立根節點 Element root = document.addElement("root"); // 獲取類中所有的欄位 Field[] fields = clz.getDeclaredFields(); // 先把List<T>物件轉成json字串 String str = JSONObject.toJSONString(list); // 把json字串轉換成List<Map<Object, Object>> List<Map<Object, Object>> mapList = (List<Map<Object, Object>>)JSONArray.parse(str); Element element; Map<Object,Object> map; // 迭代拼接xml節點資料 for (int i=0; i<mapList.size(); i++) { // 在根節點下新增子節點 element = root.addElement(clz.getSimpleName()); // 獲取Map<Object, Object>物件 map = mapList.get(i); // 從map中獲取資料,拼接xml for(Field field : fields){ // 在子節點下再新增子節點 element.addElement(field.getName()) .addAttribute("attr", field.getType().getName()) .addText(String.valueOf(map.get(field.getName()))); } } // 把xml內容輸出到檔案中 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter( new FileOutputStream(file), format); writer.write(document); System.out.println("Dom4jUtils Create Xml success!"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
物件類程式碼
package com.xm.xml.test; import java.io.Serializable; /** * 員工 * @author ouyangjun */ public class Employee implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String name; private Integer age; private Long phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Long getPhone() { return phone; } public void setPhone(Long phone) { this.phone = phone; } }
物件生成xml測試類
package com.xm.xml.test; import java.io.File; import java.util.ArrayList; import java.util.List; /** * 用dom4j把一個物件資料轉換成一個xml檔案測試類 * @author ouyangjun */ public class CreateEmployeeXml { public static void main(String[] args) { // 初始化資料 List<Employee> list = new ArrayList<Employee>(); Employee emp1 = new Employee(); emp1.setName("ouyangjun"); emp1.setAge(27); emp1.setPhone(13600425791L); Employee emp2 = new Employee(); emp2.setName("dashu"); emp2.setAge(30); emp2.setPhone(13699996666L); list.add(emp1); list.add(emp2); // 呼叫xml生成方法 Dom4jUtils.createXml(new File("E:\\employee.xml"), list, Employee.class); } }
xml生成效果圖
本章完結,待續!
本文說明:該文章屬於原創,如需轉載,請標明文章轉載來源