gpt4 book ai didi

java - Apache xmlconfig xpath 不读取元素

转载 作者:行者123 更新时间:2023-12-02 06:35:18 26 4
gpt4 key购买 nike

我想读取这个 xml 文件:

<configuration>
<parameter name="path">C:\Users\bellibot\Desktop</parameter>
<parameter name="filename">config.xml</parameter>
<module type="livestatus" id="0001">
<parameter name="username">nagiosadmin</parameter>
<parameter name="password">nagiosadmin</parameter>
<parameter name="hostaddress">127.0.0.1/nagios/live.php?q=</parameter>
</configuration>

使用此代码:

class XMLslave {
XMLConfiguration config;
String defval = "DEFAULT";


public XMLslave(){}

public XMLslave(String path, String filename){
try{
String str = path + "\\" + filename;
File file = new File(path, filename);
this.config = new XMLConfiguration(file);
this.config.setExpressionEngine(new XPathExpressionEngine());
}catch(ConfigurationException e){}
}

public static void main(String[] args) {
try{
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Insert Path");
String path = in.readLine();
System.out.println("Insert Filename");
String filename = in.readLine();
XMLslave slave = new XMLslave(path, filename);
String str = slave.getKey("path");
System.out.println(str);
}catch (IOException e){}
}

String getKey(String key){
String result;
String str = "/configuration/parameter[@name='" + key + "']";
result = this.config.getString(str);
if (result==null){
return this.defval;
}else{
return result;}
}

我不明白为什么返回值是“DEFAULT”而不是“C:\Users\bellibot\Desktop”,我测试了xpath语法并且它是合法的,我相信config.getString()在某种程度上不起作用。感谢您的帮助。

最佳答案

ExpressionEngine类的实现XMLConfiguration不需要最上面的标签(在您的情况下为 <configuration>)成为搜索表达式的一部分。将搜索表达式替换为 String str = "parameter[@name='" + key + "']";将解决您的问题。

关于java - Apache xmlconfig xpath 不读取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19730677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com