开发者

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>
开发者_如何学Python

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 ?


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


Are you trying to get all the attributes for a given node or do you want to get the attributes by name?

For the first case, you should be able to use this code:

unsigned int numAttributes = node.attributes();
for (unsigned int nAttribute = 0; nAttribute < numAtributes; ++nAttribute)
{
    pug::xml_attribute attrib = node.attribute(nAttribute);
    if (!attrib.empty())
    {
         // process here
    }
}

For the second case:

LPCTSTR GetAttribute(pug::xml_node & node, LPCTSTR szAttribName)
{
    if (szAttribName == NULL)
        return NULL;

    pug::xml_attribute attrib = node.attribute(szAttribName);
    if (attrib.empty())
        return NULL; // or empty string

    return attrib.value();
}


If you want stock plain text data into the nodes like

<name> My Name</name>

You need to make it like

rootNode.append_child("name").append_child(node_pcdata).set_value("My name");

If you want to store datatypes, you need to set an attribute. I think what you want is to be able to read the value directly right?

When you are writing the node,

rootNode.append_child("version").append_attribute("value").set_value(0.11)

When you want to read it,

rootNode.child("version").attribute("version").as_double()

At least that's my way of doing it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜