public String getValue(String file,String sysNode,String sonNode) throws ParserConfigurationException, SAXException, IOException{
String res = "";
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder=domfac.newDocumentBuilder(); //需抛出ParserConfigurationException异常
InputStream is= Thread.currentThread().getContextClassLoader().getResourceAsStream(file);//需抛出FileNotFoundException异常
Document doc=dombuilder.parse(is); //需抛出IOException异常
Element root=doc.getDocumentElement();//获取一级节点
NodeList customer=root.getChildNodes();//获取二级节点
for(int i=0;i Node info=customer.item(i);
if(sysNode.equals(info.getNodeName())){
//循环遍历二级节点下的子节点
for(Node node=info.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals(sonNode)){
//得到节点内的值
res=node.getFirstChild().getNodeValue();
break;
}
}
}
}
if(!"".equals(res)){
break;
}
}
return res;
}
String res = "";
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder=domfac.newDocumentBuilder(); //需抛出ParserConfigurationException异常
InputStream is= Thread.currentThread().getContextClassLoader().getResourceAsStream(file);//需抛出FileNotFoundException异常
Document doc=dombuilder.parse(is); //需抛出IOException异常
Element root=doc.getDocumentElement();//获取一级节点
NodeList customer=root.getChildNodes();//获取二级节点
for(int i=0;i Node info=customer.item(i);
if(sysNode.equals(info.getNodeName())){
//循环遍历二级节点下的子节点
for(Node node=info.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals(sonNode)){
//得到节点内的值
res=node.getFirstChild().getNodeValue();
break;
}
}
}
}
if(!"".equals(res)){
break;
}
}
return res;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25086534/viewspace-683371/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25086534/viewspace-683371/