Archive 17/01/2023.

[solved] XML reading problem: NextResult()

syjgin

How can I read a XML constructions such as this:

<buildings> <building name="building1" count="3"/> <building name="building2" count="3"/> <building name="building3" count="3"/> </buildings>

I’m trying to read it in this way:

XMLElement buildingsElement = rootEl.GetChild("buildings");
    XMLElement currentBuilding = buildingsElement.GetChild("building");
    while(currentBuilding)
    {
        String buildingKey = currentBuilding.GetAttribute("name");
        int buildingCount = currentBuilding.GetInt("count");
        _buildings.insert(std::pair<String,int>(buildingKey, buildingCount));
        currentBuilding = buildingsElement.GetChild("building").NextResult();
    }

…but only first “building” line was read. What’s may be wrong with NextResult() usage?

Azalrion

nextresult only works for xmlnodes from an xpath query.

syjgin

Thanks, solved by using XPath:

XPathQuery query("*/building", "ResultSet"); XPathResultSet results = query.Evaluate(rootEl); for(int i=0; i < results.Size(); i++) { String buildingKey = results[i].GetAttribute("name"); int buildingCount = results[i].GetInt("count"); _buildings.insert(std::pair<String,int>(buildingKey, buildingCount)); }