|
@@ -24,7 +24,7 @@ public class XmlUtils<T> {
|
|
/*
|
|
/*
|
|
* 查询全部xml数据
|
|
* 查询全部xml数据
|
|
* */
|
|
* */
|
|
- public List<T> readInnerXml(String path) throws Exception{
|
|
|
|
|
|
+ public List<Map> readInnerXml(String path) throws Exception{
|
|
File dir = new File(defaultPath+path);
|
|
File dir = new File(defaultPath+path);
|
|
if (!dir.exists()) {
|
|
if (!dir.exists()) {
|
|
dir.createNewFile();
|
|
dir.createNewFile();
|
|
@@ -36,7 +36,7 @@ public class XmlUtils<T> {
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Element root = dom.getRootElement();
|
|
Element root = dom.getRootElement();
|
|
- List<T> tables = new ArrayList<>();
|
|
|
|
|
|
+ List<Map> tables = new ArrayList<>();
|
|
List list = root.elements();
|
|
List list = root.elements();
|
|
//处理root节点数据
|
|
//处理root节点数据
|
|
for (int i = 0; i < list.size(); i++) {
|
|
for (int i = 0; i < list.size(); i++) {
|
|
@@ -47,7 +47,7 @@ public class XmlUtils<T> {
|
|
Element type = (Element) iterator.next();
|
|
Element type = (Element) iterator.next();
|
|
res.put(type.getName(),type.getStringValue());
|
|
res.put(type.getName(),type.getStringValue());
|
|
}
|
|
}
|
|
- tables.add((T)res);
|
|
|
|
|
|
+ tables.add(res);
|
|
}
|
|
}
|
|
return tables;
|
|
return tables;
|
|
}
|
|
}
|
|
@@ -55,26 +55,27 @@ public class XmlUtils<T> {
|
|
/*
|
|
/*
|
|
* 根据item的属性id查询xml
|
|
* 根据item的属性id查询xml
|
|
* */
|
|
* */
|
|
- public T readInnerXmlById(String path,Long id) throws Exception{
|
|
|
|
|
|
+ public T readInnerXmlById(String path,String id) throws Exception{
|
|
File dir = new File(defaultPath+path);
|
|
File dir = new File(defaultPath+path);
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Element root = dom.getRootElement();
|
|
Element root = dom.getRootElement();
|
|
- Element beQuery = (Element)root.selectSingleNode("//item[@id="+id.toString()+"]");
|
|
|
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//item[@account="+id+"]");
|
|
Map<String,Object> res = new HashMap<>();
|
|
Map<String,Object> res = new HashMap<>();
|
|
if(beQuery!=null){
|
|
if(beQuery!=null){
|
|
for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
|
|
for (Iterator iterator = beQuery.elementIterator(); iterator.hasNext();) {
|
|
Element type = (Element) iterator.next();
|
|
Element type = (Element) iterator.next();
|
|
res.put(type.getName(),type.getStringValue());
|
|
res.put(type.getName(),type.getStringValue());
|
|
}
|
|
}
|
|
|
|
+ return (T)res;
|
|
}
|
|
}
|
|
- return (T)res;
|
|
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
- * 根据item的属性id查询xml
|
|
|
|
|
|
+ * 根据item的属性account查询xml
|
|
* */
|
|
* */
|
|
- public T readInnerXmlByAccount(String path,String account) throws Exception{
|
|
|
|
|
|
+ public Map readInnerXmlByAccount(String path,String account) throws Exception{
|
|
File dir = new File(defaultPath+path);
|
|
File dir = new File(defaultPath+path);
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
@@ -86,156 +87,100 @@ public class XmlUtils<T> {
|
|
Element type = (Element) iterator.next();
|
|
Element type = (Element) iterator.next();
|
|
res.put(type.getName(),type.getStringValue());
|
|
res.put(type.getName(),type.getStringValue());
|
|
}
|
|
}
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
- return (T)res;
|
|
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
* 增加xml数据
|
|
* 增加xml数据
|
|
* */
|
|
* */
|
|
- public int writeInnerXml(String path,List<T> lists) throws Exception{
|
|
|
|
|
|
+ public int writeInnerXml(String path,T list) throws Exception{
|
|
File dir = new File(defaultPath+path);
|
|
File dir = new File(defaultPath+path);
|
|
if (!dir.exists()) {
|
|
if (!dir.exists()) {
|
|
dir.createNewFile();
|
|
dir.createNewFile();
|
|
|
|
+ Document dom = DocumentHelper.createDocument();
|
|
|
|
+ Element root = dom.addElement("table");
|
|
|
|
+ String dirpath = dir+"";
|
|
|
|
+ UtilsForXML.writeToXML(dom, dirpath);
|
|
}
|
|
}
|
|
int id = 1;
|
|
int id = 1;
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Element root = dom.getRootElement();
|
|
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());
|
|
|
|
|
|
+ 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);
|
|
UtilsForXML.writeToXML(dom, dirPath);
|
|
return id;
|
|
return id;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
- * 根据person属性id修改xml数据
|
|
|
|
|
|
+ * 根据item属性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");
|
|
|
|
|
|
+ public int UpdateXml(String path,String id ,T t) throws Exception{
|
|
|
|
+ File dir = new File(defaultPath+path);
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Element root = dom.getRootElement();
|
|
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;
|
|
|
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//item[@id="+id+"]");
|
|
|
|
+ Field[] fields = t.getClass().getDeclaredFields();
|
|
|
|
+ for(int i=0; i<fields.length; i++){
|
|
|
|
+ Field f = fields[i];
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
+ try {
|
|
|
|
+ beQuery.element(f.getName()).setText(f.get(t).toString());
|
|
|
|
+ }catch (Exception s){
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ UtilsForXML.writeToXML(dom,dirPath);
|
|
|
|
+ return 1;
|
|
}
|
|
}
|
|
|
|
+
|
|
/*
|
|
/*
|
|
- * 根据person属性id删除xml数据
|
|
|
|
|
|
+ * 根据item属性id删除xml数据
|
|
* */
|
|
* */
|
|
- public static int DeleteClinetLoginUser(int id,String appDir) throws Exception{
|
|
|
|
- File dir = new File(appDir+"\\persons.xml");
|
|
|
|
|
|
+ public int DeleteXml(String id,String path) throws Exception{
|
|
|
|
+ File dir = new File(defaultPath+path);
|
|
String dirPath = dir+"";
|
|
String dirPath = dir+"";
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Document dom = UtilsForXML.getDocument(dirPath);
|
|
Element root = dom.getRootElement();
|
|
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");
|
|
|
|
|
|
+ Element beQuery = (Element)root.selectSingleNode("//item[@id="+id+"]");
|
|
|
|
+ if(beQuery != null) {
|
|
|
|
+ beQuery.getParent().remove(beQuery);
|
|
|
|
+ UtilsForXML.writeToXML(dom, dirPath);
|
|
|
|
+ return 0;
|
|
}else {
|
|
}else {
|
|
- System.out.println("fils not found ");
|
|
|
|
- createdXml(file);
|
|
|
|
|
|
+ return 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- 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 enum XMLFILES{
|
|
|
|
+ USER("user.xml","用户信息存文件"),
|
|
|
|
+ ITEM("item.xml","商品文件");
|
|
|
|
|
|
- public void test(List<T> lists) throws Exception{
|
|
|
|
|
|
+ private String path;
|
|
|
|
+ private String remark;
|
|
|
|
+ XMLFILES(String path,String remark){
|
|
|
|
+ this.path = path;
|
|
|
|
+ this.remark = remark;
|
|
|
|
+ }
|
|
|
|
|
|
- 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 String getPath(){
|
|
|
|
+ return this.path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-// 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;
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
}
|
|
}
|