Question

i have a problem with parsing an xml document using pugiXml, it seems to me that everything is correct but this code doesn't work :(

void MainWindow::open()
{
    QString fileName = QFileDialog::getOpenFileName(this,"Open");
    xml_document doc;
    doc.load_file(fileName.toStdString().c_str());

    for (pugi::xml_node node : doc.child("Person"))
    {
        qDebug(node.child_value("nom"));
        qDebug(node.child_value("Age"));
    }
}

Xml file format :

<?xml version="1.0"?>
<Persons>
<Person>
    <nom>Med</nom>
    <Age>12</Age>
</Person>
<Person>
    <nom>Nasr</nom>
    <Age>14</Age>
</Person>
<Person>
    <nom>Souad</nom>
    <Age>52</Age>
</Person>
</Persons>
Was it helpful?

Solution

The most probable cause is that you should use doc.child("Persons").

Document object in your case has one child Persons, that has several Person children. doc.child("Person") fails to find the node and returns a null handle.

Having said that, don't forget to check load_file return value as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top