2

Here is my problem:

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<server>
    <a>true</a>
    <b>false</b>
</server>

test.cpp

sampgdk::logprintf("%d", config.get<bool>("server/a"));
sampgdk::logprintf("%d", config.get<bool>("server/b"));

result:

1
1

The result should be 1 and 0 right? But I always get 1 in both instances. It only happens to evaluate_boolean().evaluate_number() and evaluate_string() are working perfectly.

Here is my config.get

template<> bool framework::xml::get(std::string xpath) {
if (is_open) {
    try {
        return pugi::xpath_query(xpath.data()).evaluate_boolean(*ptr);
    }
    catch (std::exception &e) {
        sampgdk::logprintf("XML exception: %s\n", e.what());
    }
}
    return false;
}
  • 3
    I would expect that all bets are off since you have an invalid XML file. – user253751 Dec 17 '15 at 01:47
  • @immibis The XML file are valid. Here is my code that opening the xml file: framework::xml config; config.open("test.xml"); – Raefaldhi Amartya Dec 17 '15 at 01:50
  • 1
    The XML shown in your question is not valid. There needs to be a single enclosing tag. – Galik Dec 17 '15 at 02:31
  • @Galik May you show me what do your mean? I'm little confused here, do you meaning in test.xml file? what should i do to that file? Actually im newbie in XML. thanks – Raefaldhi Amartya Dec 17 '15 at 02:39
  • 1
    @RaefaldhiAmartya `XML` can't just be a list of tags, there must be one single tag that encloses all the other tags. Eg. `truefalse`. There is only one `` tag surrounding all the other tags. – Galik Dec 17 '15 at 06:45
  • 1
    TBH I think this may be a bug in pugixml having tried it with numbers and zero-length strings with same results. – Galik Dec 17 '15 at 13:22
  • @Galik Ok thanks for help so far, I really appreciate your help thank you!! – Raefaldhi Amartya Dec 17 '15 at 13:36
  • Have you tried first_child().evaluate_boolean(*ptr) –  Dec 17 '15 at 13:36
  • @TruthSerum : At the moment i havn't and also evaluate_boolean isn't member of first_child(), i've trying read bool from attribute and it working perfectly. sampgdk::logprintf("%d",config.ptr>child("server").child("testbool").attribute("bool").as_bool()); – Raefaldhi Amartya Dec 17 '15 at 14:03

1 Answers1

3

You are converting a string ("true") to a boolean using XPath. This returns true iff the length of the string is non-zero.

http://www.w3.org/TR/xpath/#section-Boolean-Functions

You can compare the string to "true" in the XPath expression to fix that, or to use evaluate_node(*ptr).node().text().as_bool() instead of evaluate_boolean(*ptr).

zeuxcg
  • 9,216
  • 1
  • 26
  • 33