import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
public class ReadXML {
private static File file = null;
private static Element root = null;
static {
file = new File("./config/flow.xml");
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(file);
root = doc.getRootElement();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Map<String,String> map = ReadXML.getNode("addApplyInfo");
System.out.println(map.get("next"));
}
/**
* 根据节点,返回相应的map
* map 中的键值对根据 节点的 子节点进行匹配
* key 用子节点名,value 用节点中的text值
* @param node
* @return
* @author c.chen
* @date 2017年4月26日 上午10:25:47
*/
public static Map<String,String> getNode(String node){
Map<String,String> map = new HashMap<String,String>();
Element foo;
for (Iterator i = root.elementIterator(node); i.hasNext();) {
foo = (Element) i.next();
List<Element> elList = foo.elements();
for(Element el : elList){
map.put(el.getName(), foo.elementText(el.getName()));
}
}
return map;
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans> <addApplyInfo> <text>测试0</text> <next>n0</next> <msg>测试1</msg> </addApplyInfo> </beans>