|
@@ -0,0 +1,241 @@
|
|
|
+package info.edu.utils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import info.edu.model.User;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentHelper;
|
|
|
+import org.dom4j.Element;
|
|
|
+import org.dom4j.io.SAXReader;
|
|
|
+import org.dom4j.io.XMLWriter;
|
|
|
+
|
|
|
+/*
|
|
|
+ * Created:20191218
|
|
|
+ * Xml Generic Tool Service
|
|
|
+ * */
|
|
|
+public class XmlUtils<T> {
|
|
|
+
|
|
|
+ // XML 存放路径
|
|
|
+ private final String defaultPath = "src/xml/";
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 查询全部xml数据
|
|
|
+ * */
|
|
|
+ public List<T> readInnerXml(String path) throws Exception{
|
|
|
+ File dir = new File(defaultPath+path);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.createNewFile();
|
|
|
+ Document dom = DocumentHelper.createDocument();
|
|
|
+ Element root = dom.addElement("table");
|
|
|
+ String dirpath = dir+"";
|
|
|
+ UtilsForXML.writeToXML(dom, dirpath);
|
|
|
+ }
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ List<T> tables = new ArrayList<>();
|
|
|
+ List list = root.elements();
|
|
|
+ //处理root节点数据
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Element person = (Element) list.get(i);
|
|
|
+ Map<String,Object> res = new HashMap<>();
|
|
|
+ //获取子节点数据
|
|
|
+ for (Iterator iterator = person.elementIterator(); iterator.hasNext();) {
|
|
|
+ Element type = (Element) iterator.next();
|
|
|
+ res.put(type.getName(),type.getStringValue());
|
|
|
+ }
|
|
|
+ tables.add((T)res);
|
|
|
+ }
|
|
|
+ return tables;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 根据item的属性id查询xml
|
|
|
+ * */
|
|
|
+ public T readInnerXmlById(String path,Long id) throws Exception{
|
|
|
+ File dir = new File(defaultPath+path);
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//item[@id="+id.toString()+"]");
|
|
|
+ Map<String,Object> res = new HashMap<>();
|
|
|
+ if(beQuery!=null){
|
|
|
+ for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
|
|
|
+ Element type = (Element) iterator.next();
|
|
|
+ res.put(type.getName(),type.getStringValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (T)res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 根据item的属性id查询xml
|
|
|
+ * */
|
|
|
+ public T readInnerXmlByAccount(String path,String account) throws Exception{
|
|
|
+ File dir = new File(defaultPath+path);
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//item[@account="+account+"]");
|
|
|
+ Map<String,Object> res = new HashMap<>();
|
|
|
+ if(beQuery!=null){
|
|
|
+ for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
|
|
|
+ Element type = (Element) iterator.next();
|
|
|
+ res.put(type.getName(),type.getStringValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (T)res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 增加xml数据
|
|
|
+ * */
|
|
|
+ public int writeInnerXml(String path,List<T> lists) throws Exception{
|
|
|
+ File dir = new File(defaultPath+path);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.createNewFile();
|
|
|
+ }
|
|
|
+ int id = 1;
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ for (T list : lists) {
|
|
|
+ Field[] fields = list.getClass().getDeclaredFields();
|
|
|
+ Element newPerson = root.addElement("item");
|
|
|
+ for(int i=0; i<fields.length; i++){
|
|
|
+ Field f = fields[i];
|
|
|
+ f.setAccessible(true);
|
|
|
+ if(f.getName() == "id"){
|
|
|
+ newPerson.addAttribute("id", f.get(list).toString());
|
|
|
+ }
|
|
|
+ if(f.getName() == "account"){
|
|
|
+ newPerson.addAttribute("account", f.get(list).toString());
|
|
|
+ }
|
|
|
+ Element element = newPerson.addElement(f.getName());
|
|
|
+ element.setText(f.get(list).toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UtilsForXML.writeToXML(dom, dirPath);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 根据person属性id修改xml数据
|
|
|
+ * */
|
|
|
+ public static int UpdateClinetLoginUser(int id,String appDir,String publisherName,String serverUrl,String userName,String passWord) throws Exception{
|
|
|
+ File dir = new File(appDir+"\\persons.xml");
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
|
|
|
+ beQuery.element("publishername").setText(publisherName);
|
|
|
+ beQuery.element("serverurl").setText(serverUrl);
|
|
|
+ beQuery.element("username").setText(userName);
|
|
|
+ beQuery.element("password").setText(passWord);
|
|
|
+ UtilsForXML.writeToXML(dom, dirPath);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * 根据person属性id删除xml数据
|
|
|
+ * */
|
|
|
+ public static int DeleteClinetLoginUser(int id,String appDir) throws Exception{
|
|
|
+ File dir = new File(appDir+"\\persons.xml");
|
|
|
+ String dirPath = dir+"";
|
|
|
+ Document dom = UtilsForXML.getDocument(dirPath);
|
|
|
+ Element root = dom.getRootElement();
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//person[@id="+id+"]");
|
|
|
+ beQuery.getParent().remove(beQuery);
|
|
|
+ UtilsForXML.writeToXML(dom, dirPath);
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkXml(String path){
|
|
|
+ File file = new File(defaultPath+path);
|
|
|
+ if(file.exists()){
|
|
|
+ System.out.println("file success found");
|
|
|
+ }else {
|
|
|
+ System.out.println("fils not found ");
|
|
|
+ createdXml(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void createdXml(File file){
|
|
|
+ try {
|
|
|
+ Document doc = DocumentHelper.createDocument();
|
|
|
+ FileWriter os = new FileWriter(file);
|
|
|
+ doc.write(os);
|
|
|
+ os.flush();
|
|
|
+ os.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("文件创建失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void test(List<T> lists) throws Exception{
|
|
|
+
|
|
|
+ for (T list : lists) {
|
|
|
+ Field[] fields = list.getClass().getDeclaredFields();
|
|
|
+ for(int i=0; i<fields.length; i++){
|
|
|
+ Field f = fields[i];
|
|
|
+ f.setAccessible(true);
|
|
|
+ System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(list));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// public void writeInnerXml(String path, List<T> lists) throws Exception{
|
|
|
+//
|
|
|
+// List<T> oldRes = new ArrayList<>();
|
|
|
+// // 提取原有数据
|
|
|
+// oldRes = readInnerXml(path);
|
|
|
+// if (oldRes == null){
|
|
|
+// System.out.println("not relast");
|
|
|
+// }
|
|
|
+// Document doc = DocumentHelper.createDocument();
|
|
|
+// Element ele = doc.s("table");
|
|
|
+//
|
|
|
+// for (T list : lists) {
|
|
|
+// Field[] fields = list.getClass().getDeclaredFields();
|
|
|
+// for(int i=0; i<fields.length; i++){
|
|
|
+// Field f = fields[i];
|
|
|
+// f.setAccessible(true);
|
|
|
+// ele.setA("id", "1");
|
|
|
+// Element element = ele.addElement(f.getName());
|
|
|
+// element.setText(f.get(list).toString());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// XMLWriter writer = new XMLWriter(new FileWriter(new File(path)));
|
|
|
+// writer.write(doc);
|
|
|
+// writer.close();
|
|
|
+// } catch (Exception e) {
|
|
|
+// // TODO: handle exception
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+// public List<T> readInnerXml(String path) {
|
|
|
+// try {
|
|
|
+// File file = new File(path);
|
|
|
+// SAXReader reader = new SAXReader();
|
|
|
+// Document doc = reader.read(file);
|
|
|
+// Element root = doc.getRootElement();
|
|
|
+// List<T> lists = new ArrayList<>();
|
|
|
+//// root.
|
|
|
+// for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
|
|
|
+// Map<String,Object> res = new HashMap<>();
|
|
|
+// Element type = (Element) iterator.next();
|
|
|
+// res.put(type.getName(),type.getStringValue());
|
|
|
+// lists.add((T)res);
|
|
|
+// }
|
|
|
+// return lists;
|
|
|
+// } catch (Exception e) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// }
|
|
|
+}
|