2016-09-24 147 views
-1

我連接到許多服務器的數據庫以從它們檢索所有數據,我使用xml文件包含每個數據庫服務器[ip,port,user,passwd]的信息,然後查詢在該服務器上執行。我在我的代碼中讀取查詢作爲arrayList,並逐個獲取它們以進行處理。我需要的是一個清晰簡單的代碼,以便讀取數據庫服務器信息以連接到它並執行相關查詢,即每次我需要在我的代碼中獲取數據庫IP,端口,用戶,密碼的查詢。這裏是我的xml文件結構。非常感謝。使用java代碼讀取xml節點

<?xml version="1.0"?> 
<Database> 
<DB> 
<ip></ip> 
<port></port> 
<user></user> 
<pass></pass> 
</DB> 
<Query> 
select date from myTable 
</Query> 
<DB> 
<ip></ip> 
<port></port> 
<user></user> 
<pass></pass> 
</DB> 
<Query> 
select time from myTable 
</Query> 
<DB> 
<ip></ip> 
<port></port> 
<user></user> 
<pass><></pass> 
</DB> 
<Query> 
select name from myTable 
</Query> 
</Database> 

回答

0

您可以使用Java的DOM api進行xml處理和xpath。

下面是一些示例代碼,做你想要什麼部分(提取IP和端口)。您將需要修改它做休息:

import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathExpressionException; 
import javax.xml.xpath.XPathFactory; 

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 DomTester { 


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { 

     InputStream inStr = null; 
     try { 
      String xml = "<?xml version=\"1.0\"?>" 
        + "<Database>" 
        + "<DB>" 
        +  "<ip>666</ip>" 
        +  "<port>7</port>" 
        + "</DB>" 
        + "<DB>" 
        + " <ip>13</ip>" 
        +  "<port>1</port>" 
        + "</DB>" 
        + "</Database>"; 

      inStr = new ByteArrayInputStream(xml.getBytes()); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(inStr); 

      XPathFactory xFactory = XPathFactory.newInstance(); 
      XPath xpath = xFactory.newXPath(); 
      XPathExpression expr = xpath.compile("/Database"); 
      Element databaseEl = (Element)expr.evaluate(doc, XPathConstants.NODE); 

      NodeList databaseNodeList = databaseEl.getChildNodes(); 
      for (int ii=0; ii<databaseNodeList.getLength(); ++ii) { 
       Node dbNode = databaseNodeList.item(ii); 
       XPathExpression ipExpr = xpath.compile("ip"); 
       Element ipElement = (Element)ipExpr.evaluate(dbNode, XPathConstants.NODE); 
       String ip = ipElement.getTextContent(); 

       XPathExpression portExpr = xpath.compile("port"); 
       Element portEl = (Element)portExpr.evaluate(dbNode, XPathConstants.NODE); 
       String port = portEl.getTextContent(); 

       System.out.println("DB node[" + ii + "] = ip: " + ip + " port: " + port); 
      } 
     } finally { 
      if (null != inStr) { 
       inStr.close(); 
      } 
     } 
    } 
} 

因爲你是從文件中讀取,而不是使用字符串,而不是做這樣的:

inStr = new ByteArrayInputStream(xml.getBytes()); 

做到這一點:

File f = new File("your/file/path.xml"); 
inStr = new FileInputStream(f);