0

I want to read this xml file:

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

using this code:

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;}
    }

I don't understand why the return value is "DEFAULT" instead of "C:\Users\bellibot\Desktop", i tested the xpath syntax and it's legal, i belive config.getString() is somehow not working. Thanks for the help.

  • There are two things that you could try: a) append `/text()` to your XPath expression in the assignent to `str` and b) prepend a valid XML declaration (e.g. ``) to your input XML. – Marcus Rickert Nov 02 '13 at 01:15
  • Tried both with no luck, thanks anyway. – user2945536 Nov 02 '13 at 10:59
  • One more idea: in another ExpressionEngine implementation for property style notation the top most tag (which would be `` in your case) is not part of the search expression. Could you try with `String str = "parameter[@name='" + key + "']";`? – Marcus Rickert Nov 02 '13 at 15:41
  • I derived an answer from our conclusion. Could you pick it? – Marcus Rickert Nov 04 '13 at 23:23

1 Answers1

2

The ExpressionEngine implementations for class XMLConfiguration do not require the top most tag (which would be <configuration> in your case) to be part of the search expression. Replacing your search expression by String str = "parameter[@name='" + key + "']"; will fix your problem.

Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29