日期:2014-05-16 浏览次数:20577 次
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ParseXml { /** 解析数据库信息的配置文件 * 返回配置文件中的信息列表 * @param args * @see [类、类#方法、类#成员] */ public static List parseXml(File dbConfigXmlFile) { List dbConfList = new ArrayList(); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(dbConfigXmlFile); NodeList nodes = doc.getElementsByTagName("local-tx-datasource"); NodeList subNodes = null; Node tempNode = null; String nodeValue = null; String[] infos = null; for(int i=0;i<nodes.getLength();i++) { tempNode = nodes.item(i); if(tempNode instanceof Element) { tempNode = (Element)tempNode; } subNodes = tempNode.getChildNodes(); infos = new String[5]; for(int j =0;j<subNodes.getLength();j++) { tempNode = subNodes.item(j); if(tempNode instanceof Element) { tempNode = (Element) tempNode; nodeValue =tempNode.getFirstChild().getNodeValue(); if(tempNode.getNodeName().equals("connection-url")) { infos[0]=nodeValue; } if(tempNode.getNodeName().equals("driver-class")) { infos[1]=nodeValue; } if(tempNode.getNodeName().equals("user-name")) { infos[2]=nodeValue; } if(tempNode.getNodeName().equals("password")) { infos[3]=nodeValue; } if(tempNode.getNodeName().equals("isUse")) { infos[4]=nodeValue; } } } dbConfList.add(infos); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } return dbConfList; } }
?