【发布时间】:2015-12-03 18:32:53
【问题描述】:
我想从 XML 文件中读取数据。我正在使用 Java 和 Selenium WebDriver。我在研究时找到了很多解决方案。问题似乎不适用于我的问题。
我的 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。
请帮助..在此先感谢!
【问题讨论】:
-
您的 xml 文件根标签为 ="Enviroment"。但在您的代码中,您寻找:“环境”。有拼写错误
-
谢谢..@Mahsum 我更正了,但它仍然显示输出为
null -
您提供的 xml 文件是否正确?当我运行它时,得到关于无效的不同错误?
-
@Mahsum Xml 文件正确。请让我知道您遇到的错误是什么。对我来说,它总是以 null 结束
-
prop.loadFromXML(fileInput); 出现以下错误=> 线程“主”java.util.InvalidPropertiesFormatException 中的异常:org.xml.sax.SAXParseException;行号:3;列号:14;必须声明元素类型“环境”。
标签: java xml selenium selenium-webdriver properties-file