67,513
社区成员
发帖
与我相关
我的任务
分享
package cn.ccb.common.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadXml {
private Document getDocument(String xmlUrl) {
Document document = null;
SAXReader saxReader = new SAXReader();
FileInputStream file;
try {
file = new FileInputStream(xmlUrl);
InputStreamReader reader = new InputStreamReader(file, "UTF-8");
document = saxReader.read(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
// SAXReader reader = new SAXReader();
// Document document = null;
// try {
// document = reader.read(new File(xmlUrl));
// } catch (DocumentException e) {
// e.printStackTrace();
// } catch (MalformedURLException e) {
// e.printStackTrace();
// }
return document;
}
/**
* 获取当前步骤号对应的部门ID
*
* @param xmlUrl
* 获取XML地址
* @param step
* 步骤号
* @return 部门ID
*/
public String getRole(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver");
return elt.attributeValue("role");
}
/**
* 通过当前step查询下一步对应的角色
*
* @param xmlUrl
* @param step
* @return
*/
public String getNextRole(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver/target/choice/sender");
return elt.attributeValue("role");
}
/**
* 通过当前step查询下一步对应的step
*
* @param xmlUrl
* @param step
* @return
*/
public String getNextStep(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver/target/choice/sender");
return elt.attributeValue("step");
}
/**
* 获取下一步发送部门数组
*
* @param xmlUrl
* XML地址
* @param step
* 当前步骤号
* @param node
* choice or split
* @return Map<role,step>
*/
public Map getNextStepItems(String xmlUrl, String step, String node) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step[@id='" + step
+ "']/receiver/target/" + node + "/sender");
Map map = new HashMap();
Element elt;
if (null != list) {
for (int i = 0; i < list.size(); i++) {
elt = (Element) list.get(i);
map.put(elt.attributeValue("role"), elt.attributeValue("step"));
}
}
return map;
}
/**
* 通过角色ID获取步骤号
*
* @param xmlUrl
* @param role
* 角色ID
* @return 步骤号
*/
public String getStepByRole(String xmlUrl, String role) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step");
String step = "";
Element elt;
if (null != list) {
for (int i = 0; i < list.size(); i++) {
elt = (Element) list.get(i);
if (role.equals(elt.element("receiver").attribute("role"))) {
step = elt.attributeValue("id");
break;
}
}
}
return step;
}
/**
* 获取当前步骤需要的直线数
*
* @param xmlUrl
* @param step
* @return
*/
public String getNeedStepNum(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step[@id='" + step
+ "']/receiver/join/target-from");
Element elt;
String str = "";
if (null == list || 0 == list.size()) {
return "";
} else {
elt = (Element) list.get(0);
str = elt.attributeValue("step");
for (int i = 1; i < list.size(); i++) {
elt = (Element) list.get(i);
str = str + "," + elt.attributeValue("step");
}
return str;
}
}
public String getDescriptionByStep(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']");
return elt.attributeValue("description");
}
}