Java调用外部xml配置文件来做些:检测
InputStream is = Student.class.getResourceAsStream("/student.xml"); //获取配置文件
if(is == null) throw new Exception("打不到指定文件。");
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(is); //org.dom4j....
Element branch = (Element)doc.selectSingleNode("/root/branch"); // branch节点
String pm_code = getElementValue(branch,"code");
//getElementValue(Element e,String name); // 值=getElementValue(元素,元素中单个属性名)
String pm_action = getElementValue(branch,"action");
遍历XML中数据:
List maps = doc.selectNodes("/root/alldata/data");
for(int i=0; i<maps.size(); i++)
{
Element map = (Element) maps.get(i); //循环得到每个元素,即节点
String ccCodeValue = getElementValue(map,"ccCode"); //取得值
String idCodeValue = getElementValue(map,"idcode");
......
//小心空数据
}
<?xml version="1.0" encoding='GBK'?>
<root>
<branch code="3210" action="check"/>
<alldata>
<!--ccCode-县资助中心代码,idcode-身份证号码-->
<data ccCode="" idcode="">
</alldata>
</root>