Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsin XML file using pugixml

Hi I want to use XML file as a config file, from which I will read parameters for my application. I came across on PugiXML library, however I have problem with getting values of attributes. My XML file looks like that

<?xml version="1.0"?>
<settings>
    <deltaDistance> </deltaDistance>
    <deltaConvergence>0.25 </deltaConvergence> 
    <deltaMerging>1.0 </deltaMerging> 
    <m> 2</m>
    <multiplicativeFactor>0.7 </multiplicativeFactor> 
    <rhoGood> 0.7 </rhoGood>
    <rhoMin>0.3 </rhoMin>
    <rhoSelect>0.6 </rhoSelect>
    <stuckProbability>0.2 </stuckProbability> 
    <zoneOfInfluenceMin>2.25 </zoneOfInfluenceMin>
</settings>

To pare XML file I use this code

void ReadConfig(char* file)
{
    pugi::xml_document doc;
    if (!doc.load_file(file)) return false;

    pugi::xml_node tools = doc.child("settings");

    //[code_traverse_iter
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        cout<<it->name() <<    " " <<    it->attribute(it->name()).as_double();  
    }

}

and I also was trying to use this

void ReadConfig(char* file)
{
    pugi::xml_document doc;
    if (!doc.load_file(file)) return false;

    pugi::xml_node tools = doc.child("settings");

    //[code_traverse_iter
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        cout<<it->name() <<    " " <<    it->value();
    }

}

Attributes are loaded corectly , however all values are equals 0. Could somebody tell me what I do wrong ?

like image 671
george Avatar asked Nov 19 '25 08:11

george


1 Answers

I think your problem is that you're expecting the value to be stored in the node itself, but it's really in a CHILD text node. A quick scan of the documentation showed that you might need

it->child_value()

instead of

it->value()
like image 83
Jim Garrison Avatar answered Nov 21 '25 23:11

Jim Garrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!