2015-09-08 41 views
0

我想從XML文件讀取數據。我正在使用Selenium WebDriver Java &。我在研究過程中找到了許多解決方案。問題似乎沒有適用於我的問題。讀取硒中的xml數據

我的XML文件是這樣:

<Enviroment> 
    <Parameter>Test_Url</Parameter> 
    <value>https://www.google.com</value> 
    <Parameter>Distributed_Test</Parameter> 
    <value>no</value> 
    <Parameter>Result_Name</Parameter> 
    <value>Google_Results</value> 
</Enviroment> 

我使用讀取此xml文件的代碼是在這裏。

public class ReadXML { 
static String value; 

public static void main(String[] args) throws FileNotFoundException,IOException { 

File file = new File("path of the file"); 
FileInputStream fileInput =new FileInputStream(file); 
Properties prop =new Properties(); 

//prop.load(fileInput); 
prop.loadFromXML(fileInput); 

fileInput.close(); 
Enumeration enumKeys=prop.keys(); 

while(enumKeys.hasMoreElements()){ 
    //String node = "Environment"; 
    String subnode= "Parameter"; 
    if(((String) enumKeys.nextElement()).contains(subnode)){ 
     value = prop.getProperty(subnode); 
     System.out.println(value); 
    } 
} 
return ; 
} 
  • 在我使用prop.load(fileInput),輸出打印爲null三次三個參數值,相信。

  • 但是,如果我使用prop.loadFromXML(fileInput),InvalidPropertiesFormatException顯示。

請幫助..謝謝!

+0

XML文件的根標籤= 「環境」。但是在你的代碼中,你需要尋找:「環境」。有錯誤 –

+0

謝謝.. @ Mahsum我糾正了,但它仍然顯示輸出爲'null' –

+0

您呈現的xml文件是否正確?當我運行它,得到不同的錯誤關於無效? –

回答

0

在屬性格式的誤差可以通過xmllint進行檢查:

xmllint foo.properties 

在這種情況下,該結束標記是:

</Enviroment> 

但需要是:

</Environment> 

參考文獻