版本2
parent
c30240a01f
commit
e94b5d88fe
@ -0,0 +1,73 @@
|
|||||||
|
package com.manage.utils;
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentHelper;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回结果工具类
|
||||||
|
*/
|
||||||
|
public class ResultUtils {
|
||||||
|
/**
|
||||||
|
* 通过document对象返回节点对象
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Element getElement(Document response){
|
||||||
|
Element element = response.getRootElement();
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功返回
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Document success(){
|
||||||
|
// 1、创建document对象
|
||||||
|
Document document= DocumentHelper.createDocument();
|
||||||
|
//2.创建根节点
|
||||||
|
Element response=document.addElement("Response");
|
||||||
|
// 3、生成子节点及子节点内容
|
||||||
|
response.addElement("ResultCode").setText("0");
|
||||||
|
response.addElement("ResultCount").setText("成功");
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败返回
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String fail(){
|
||||||
|
// 1、创建document对象
|
||||||
|
Document document= DocumentHelper.createDocument();
|
||||||
|
//2.创建根节点
|
||||||
|
Element response=document.addElement("Response");
|
||||||
|
// 3、生成子节点及子节点内容
|
||||||
|
response.addElement("ResultCode").setText("1");
|
||||||
|
response.addElement("ResultCount").setText("失败");
|
||||||
|
// //赋值
|
||||||
|
// resCode.setText(String.valueOf(Codes.ERROR.getCode()));
|
||||||
|
// retCon.setText(Codes.ERROR.getMessage());
|
||||||
|
return document.asXML();
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 失败返回
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static String fail(String message){
|
||||||
|
// // 1、创建document对象
|
||||||
|
// Document document= DocumentHelper.createDocument();
|
||||||
|
// //2.创建根节点
|
||||||
|
// Element response=document.addElement(Codes.RESPONSE.getMessage());
|
||||||
|
// // 3、生成子节点及子节点内容
|
||||||
|
// Element RetInfo = response.addElement(Codes.RET_INFO.getMessage());
|
||||||
|
// //4.生成代码和描述节点
|
||||||
|
// Element resCode = RetInfo.addElement(Codes.RET_CODE.getMessage());
|
||||||
|
// Element retCon = RetInfo.addElement(Codes.RET_CON.getMessage());
|
||||||
|
// //赋值
|
||||||
|
// resCode.setText(String.valueOf(Codes.ERROR.getCode()));
|
||||||
|
// retCon.setText(StringUtils.isNotBlank(message)?message: Codes.ERROR.getMessage());
|
||||||
|
// return document.asXML();
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,134 @@
|
|||||||
|
package com.manage.utils;
|
||||||
|
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
import org.dom4j.io.SAXReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class XmlUtils {
|
||||||
|
//定义解析器和文档对象
|
||||||
|
private SAXReader saxReader;
|
||||||
|
private Document document;
|
||||||
|
|
||||||
|
public XmlUtils(String path) {
|
||||||
|
//获取解析器
|
||||||
|
saxReader = new SAXReader();
|
||||||
|
try {
|
||||||
|
//获取文档对象
|
||||||
|
document = saxReader.read(path);
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlUtils(InputStream path) {
|
||||||
|
//获取解析器
|
||||||
|
saxReader = new SAXReader();
|
||||||
|
try {
|
||||||
|
//获取文档对象
|
||||||
|
document = saxReader.read(path);
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态获取节点内容
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getElement(String name) {
|
||||||
|
//获取根节点
|
||||||
|
Element root = document.getRootElement();
|
||||||
|
// Object directory = FileUtils.getJsonByName(Codes.DIRECTORY.getMessage());
|
||||||
|
// Element current = root;
|
||||||
|
// if (directory == null) {
|
||||||
|
// throw new RuntimeException("没有定义目录结构");
|
||||||
|
// }
|
||||||
|
// List<String> directoryArr = (List<String>) directory;
|
||||||
|
// for (String dire : directoryArr) {
|
||||||
|
// current = current.element(dire);
|
||||||
|
// }
|
||||||
|
return root.element(name) == null ? "" : root.element(name).getText();
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 根据路径动态获取节点
|
||||||
|
// *
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public Element getElement(List<String> directory) {
|
||||||
|
// //获取根节点
|
||||||
|
// Element root = document.getRootElement();
|
||||||
|
//
|
||||||
|
// Element current = root;
|
||||||
|
// List<String> directoryArr = directory;
|
||||||
|
// for (String dire : directoryArr) {
|
||||||
|
// current = current.element(dire);
|
||||||
|
// }
|
||||||
|
// if (current == null) {
|
||||||
|
// throw new RuntimeException("未找到对应节点");
|
||||||
|
// }
|
||||||
|
// return current;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 返回存在的根节点
|
||||||
|
// */
|
||||||
|
// public List<Element> getJsonByName() {
|
||||||
|
// //获取目录结构
|
||||||
|
// String path = FileUtils.currentPath();
|
||||||
|
// //解析json映射文件
|
||||||
|
// String json = com.docus.bgts.utils.JsonUtils.readJsonFile(path + Codes.JSON_ADDRESS.getMessage());
|
||||||
|
// Map jsonMap = JSON.parseObject(json, Map.class);
|
||||||
|
// //判断是否多条
|
||||||
|
// List<String> basicArr = (List<String>) jsonMap.get("doubleBasic");
|
||||||
|
// List<String> directory = (List<String>) jsonMap.get("basicDirectory");
|
||||||
|
// List<Element> elements = null;
|
||||||
|
// Element root = this.getElement(directory);
|
||||||
|
// for (String basic : basicArr) {
|
||||||
|
// elements = root.elements(basic);
|
||||||
|
// if (elements != null && elements.size() > 0) {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (elements == null || elements.size() == 0) {
|
||||||
|
// //只有一条
|
||||||
|
// List<String> rootDirectory = (List<String>) jsonMap.get("directory");
|
||||||
|
// root = this.getElement(rootDirectory);
|
||||||
|
// elements = new ArrayList<>();
|
||||||
|
// elements.add(root);
|
||||||
|
// }
|
||||||
|
// return elements;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 根据节点名称获取内容
|
||||||
|
// *
|
||||||
|
// * @param name 节点名称
|
||||||
|
// * @return 节点内容
|
||||||
|
// */
|
||||||
|
// public String getElementText(String name) {
|
||||||
|
// //定位根节点
|
||||||
|
// Element root = document.getRootElement();
|
||||||
|
// //根据名称定位节点
|
||||||
|
// Element msg = root.element(Codes.MSG.getMessage());
|
||||||
|
// if (msg == null) {
|
||||||
|
// throw new RuntimeException("没有" + Codes.MSG.getMessage() + "节点");
|
||||||
|
// }
|
||||||
|
//// Element patInfo = msg.element(Codes.PAT_INFO.getMessage());
|
||||||
|
//// if(patInfo==null){
|
||||||
|
//// throw new RuntimeException("没有"+Codes.PAT_INFO.getMessage()+"节点");
|
||||||
|
//// }
|
||||||
|
// Element element = msg.element(name);
|
||||||
|
// if (element == null) {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
// //返回节点内容
|
||||||
|
// return element.getText();
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue