java 解析xml

//这是java类

public class AAA {
 private NodeList nodeList=null;
public AAA() {
 try{
  DocumentBuilderFactory documentBuilderfactory = DocumentBuilderFactory.newInstance();
  documentBuilderfactory.setValidating(false);
  DocumentBuilder documentBuilder = documentBuilderfactory.newDocumentBuilder();
  File file=new File(path+"config.xml");
  Document document= documentBuilder.parse(file);
  nodeList=document.getElementsByTagName("MM").item(0).getChildNodes();
 }catch(Exception e){
  e.printStackTrace();
 }
 }
public String getFirstTableName() {
 if (nodeList != null) {
  try {
   for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE
      && node.getNodeName().equals("tablename")) {
         return node.getTextContent();
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("读取节点错误!!!");
  }
 }
 return null;
}
}

//web-inf 下的config.xml 连接池文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <MM>
  <jndi-name>java:comp/env/jdbc/mingzi</jndi-name>
     <tablename>test</tablename>
 </MM>
</config>

你可能感兴趣的:(java,xml,连接池)