JbpmUtil.java

本文介绍了一个针对JBPM流程引擎的XML配置文件解析工具类。该工具使用DOM4J库来读取和解析XML文件,提供了获取根节点、子节点及节点属性等方法,便于开发者快速获取流程定义中的关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.huike.leave.service.util; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

import org.dom4j.Attribute; 
import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.Element; 
import org.dom4j.Node; 
import org.dom4j.io.SAXReader; 

public class JbpmUtil { 

private SAXReader reader = new SAXReader();   
private Document document = null; 
private Element rootElement = null; 
private Element childElement = null; 
private Attribute attribute = null; 
private String attribute_value = null; 

private String node_name_property = null; 

public JbpmUtil(String filepath){ 
this.parseXML(filepath); 
this.getRootElement(); 


public void parseXML(String filepath) { 
try { 
document = reader.read(new File(filepath)); 
if(filepath == null) 
p("SAXReader解析时出现异常,可能指定路径下的文件不存在"); 
} catch (DocumentException e) { 
p("SAXReader解析时出现异常,可能指定路径下的文件不存在"); 
e.printStackTrace(); 
}   


public Element getRootElement(){ 
rootElement = document.getRootElement(); 
return rootElement; 


public String getRootElementKey(){ 
return rootElement.attributeValue("name"); 

// public  String getRootElementKey(String attributeName) { 
// return  rootElement.attributeValue(attributeName); 
// } 

// public Element getProcessChildElement(String nodeName){ 
// childElement = rootElement.element(nodeName); 
// return childElement; 
// } 
// @SuppressWarnings({ "rawtypes" }) 
// public List<Element> getChildElementAsList(Element parentElement){ 
// List<Element> elementAsList = new ArrayList<Element>(); 
// for(Iterator it=parentElement.elementIterator();it.hasNext();) 
// {   
// childElement = (Element) it.next(); 
// elementAsList.add(childElement); 
// } 
// return elementAsList;   
// } 

/** 
* 获取根节点下第一层子节点指定的的属性名的对应值 
* @param nodeName 
* @param attributeName 
* @return 
* @throws Exception 
*/ 
public String getProperty(String nodeName,String attributeName)throws Exception{ 
Element currentElement = rootElement.element(nodeName); 
if(currentElement == null) 

new Exception("文件中不存在指定的节点名"); 
System.out.println("文件中不存在指定的节点名"); 

else{ 
attribute = currentElement.attribute(attributeName); 


    if(attribute == null) 
    { 
    System.out.println("指定的属性在此节点中不存在,请检查传入的属性名与节点名是否匹配"); 
    new Exception("指定的属性在此节点中不存在,请检查传入的节点名与属性名是否匹配"); 
    } 
    else{ 
    attribute_value = attribute.getValue(); 
    return attribute_value; 
    } 
    return attribute_value; 


// /** 
// * 处理第二层子节点下只有一个元素 
// */ 
// public String getProperty(String firstLevelNodeName,String firstLevelChildNodeName,String attributeName){ 
// Element start = rootElement.element(firstLevelNodeName); 
// Element transition = start.element(firstLevelChildNodeName); 
// Attribute a = transition.attribute(attributeName); 
// return a.getValue(); 
// } 
// /** 
// * 处理第二层子节点下有多个元素 
// */ 
// @SuppressWarnings({ "unchecked", "rawtypes" }) 
// public List getProperties (String firstLevelChildNodeName,String secondLevelChildNodeName,String attributeName){ 
// Element task = rootElement.element(firstLevelChildNodeName); 
// List task_transitionAsList = task.elements(secondLevelChildNodeName); 
// Element transition = null; 
// Attribute attribute = null; 
// String s = null; 
// List list = new ArrayList(); 
// for(int i=0; i<task_transitionAsList.size();i++) 
// { 
// transition = (Element) task_transitionAsList.get(i); 
// attribute = transition.attribute(attributeName); 
// s = attribute.getText(); 
// list.add(s); 
// } 
// return list; 
// } 

// public String getNameProperty(String nodeName){ 
// Element currentElement = rootElement.element(nodeName); 
//     attribute = currentElement.attribute("name"); 
//     attribute_value = attribute.getValue(); 
// return node_name_property; 
// } 

// @SuppressWarnings("rawtypes") 
// public List getCurrentNodeInformation(String nodeName){ 
// return  rootElement.element(nodeName).attributes(); 
// } 
/** 
* 根据元素的name属性值获取对应的元素 
* @param name 
* @return 
*/ 
@SuppressWarnings("rawtypes") 
public Element getElementByNameAttributeValue(String name){ 
Iterator it = rootElement.elementIterator(); 
Element e = null; 
Attribute a = null; 
List<Element> el = new ArrayList<Element>(); 
while(it.hasNext()) 

e = (Element)it.next(); 
a = e.attribute("name"); 
if(a.getText().equals(name)) 

el.add(e); 
if(el.size() == 1) 

return el.get(0); 


if(el.size()==0) 
System.out.println("根元素下的子元素中存在具有 name属性值为"+name+"的元素"); 

return el.get(0); 


// @SuppressWarnings("rawtypes") 
// public String getTransitionToProperty(String currentActivityName, String decisionInfo){ 
// Element currentElment = this.getElementByNameAttributeValue(currentActivityName); 
// List transitionAsList = currentElment.elements(); 
// Element e = null; 
// String toValue = null ; 
// for(int i=0; i<transitionAsList.size();i++) 
// { 
// e = (Element)transitionAsList.get(i); 
// toValue = e.attribute("name").getText().toString(); 
// p(toValue); 
// p(decisionInfo); 
// if(decisionInfo.equals(toValue)) 
// { 
// toValue = e.attribute("to").getText().toString(); 
// return toValue; 
// } 
// } 
// return toValue; 
// } 
public static void p(Object o){ 
System.out.println(o.toString()); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值