简体   繁体   English

读取硒中的xml数据

[英]read xml data in selenium

I want to read data from an XML file. 我想从XML文件读取数据。 I am using Java & Selenium WebDriver. 我正在使用Java&Selenium WebDriver。 I have found many solutions while researching. 我在研究时发现了许多解决方案。 The problem is none seems to apply to my problem. 这个问题似乎与我的问题无关。

My XML file is as such : 我的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>

The code I am using to read this xml file is here. 我用来读取此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 ;
}
  • When I am using prop.load(fileInput) , output is printed as null thrice for the three parameter values, I believe. 当我使用prop.load(fileInput)输出打印为null的三个参数值三次,我相信。

  • But if I use prop.loadFromXML(fileInput) , InvalidPropertiesFormatException is shown. 但是,如果我使用prop.loadFromXML(fileInput) ,则会显示InvalidPropertiesFormatException

Please help..Thanks in Advance!! 请帮助。。在此先谢谢!!

The error in the properties format can be checked via xmllint: 可以通过xmllint检查属性格式中的错误:

xmllint foo.properties

In this case, the closing tag is: 在这种情况下,结束标记为:

</Enviroment>

but needs to be: 但必须是:

</Environment>

References 参考文献

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM