在Web应用中使用XML文件配置数据源
来源: 时间:06-12-30 点击: 点击这里收藏本文
*/
protected Element root;
protected void init(ServletContext sctx, String xmlFile) throws Exception {
InputStream is=null;
try{
is=sctx.getResourceAsStream(xmlFile);
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(new InputSource(is));
root=doc.getDocumentElement();
System.out.println("root: "+root );
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
is.close();
}
}
}
protected String getElementText(Element parent,String name){
NodeList nodeList=parent.getElementsByTagName(name);
if(nodeList.getLength()==0){
return null;
}
Element element=(Element)nodeList.item(0);
StringBuffer sb=new StringBuffer();
for(Node child=element.getFirstChild();child!=null;child=child.getNextSibling()){
if(child.getNodeType()==Node.TEXT_NODE){
sb.append(child.getNodeValue());
}
}
return sb.toString().trim();
把此文章收藏到: