首先引入dom4j的包
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
xml格式和实现的功能为一个工单中有3个级别,需要级联操作
所以写出如下的xml格式
<?xml version="1.0" encoding="UTF-8"?>
<types>
<type1 value="type11">
<type2 value="type111">
<type3 value="充值" />
<type3 value="提现" />
<type3 value="转账" />
</type2>
</type1>
<type1 value="type12">
<type2 value="type121">
<type3 value="开通问题" />
<type3 value="账户问题" />
</type2>
<type2 value="type122">
<type3 value="转入" />
<type3 value="转出" />
</type2>
</type1>
</types>
获取级别1的方法
@SuppressWarnings("all")
public List<String> getAllFirstBizTypes() {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(businessRoot);
List type1s = document.selectNodes("//types/type1"); //得到types下的所有type1,及可以得到类型1中的element
List<String> buses = new ArrayList<String>();
for (Object type1Node : type1s) {
Element type1Element = (Element)type1Node;
buses.add(type1Element.attributeValue("value")); //得到element中的属性的值的方法
}
return buses;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
下面的方法是直接得到级别三的方法,因为级别二的方法使用jar包的方法一样所以就直接列举此方法了
@SuppressWarnings("all")
public List<String> getThirdBizTypesBySecondBiz(String firstBiz,String secondBiz) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(businessRoot);
List type3s = document.selectNodes("//types/type1[@value='"+firstBiz+"']/type2[@value='"+secondBiz+"']/type3"); //通过@配置可以匹配到此属性的值
List<String> buses = new ArrayList<String>();
for (Object type3Node : type3s) {
Element type3Element = (Element)type3Node;
buses.add(type3Element.attributeValue("value"));
}
return buses;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
另一个用xml实现的组和用户的树形结构,为了实现功能为,取出此用户同组的人和此用户所在组的子组的人
<?xml version="1.0" encoding="UTF-8"?>
<teams>
<team value="support_personal">
<user value="user1"></user>
<user value="user2"></user>
<user value="user3"></user>
</team>
<team value="support_merchant">
<user value="user4"></user>
<user value="user5"></user>
<user value="user6"></user>
</team>
<team value="support_others" children="balance_risk_sales_technology">
<user value="user7"></user>
<user value="user8"></user>
<user value="user9"></user>
</team>
<team value="support_others_balance">
<user value="user10"></user>
<user value="user11"></user>
<user value="user12"></user>
</team>
<team value="support_others_risk">
<user value="user13"></user>
<user value="user14"></user>
</team>
<team value="support_others_sales">
<user value="user15"></user>
<user value="user16"></user>
</team>
<team value="support_others_technology">
<user value="user17"></user>
<user value="user18"></user>
</team>
</teams>
@SuppressWarnings("all")
public List<String> getUser(String teamName,String currName){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(userRoot);
List teams = document.selectNodes("//teams/team[@value='"+teamName+"']");//根据属性的值得到当前用户的所在组
List<String> userNames = new ArrayList<String>();
for(int i=0;i<teams.size();i++){
Element teamElement = (Element)teams.get(i);
String teamCurrName = teamElement.attributeValue("value");
List users = teamElement.selectNodes("user"); //根据此element得到子user的element的集合
for (Object user : users) {
Element userElement = (Element)user;
String userName = userElement.attributeValue("value");
if(userName!=null && !"".equals(userName) && !currName.equals(userName)){ //把当前所在组的人加入list
userNames.add(userName);
}
}
String childrenTeamString = teamElement.attributeValue("children");
if(childrenTeamString!=null){
for (String childTeamString : childrenTeamString.split("_")) { //遍历孩子加入到list循环,不使用递归
teams.add(document.selectSingleNode("//teams/team[@value='"+teamCurrName+"_"+childTeamString+"']"));
}
}
}
return userNames;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}