读取XML文件信息

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;
 }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25086534/viewspace-683371/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25086534/viewspace-683371/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值