2

I'm trying to read an XML file with the structure

<name>search_name</name>
<Polygon>
    <outerBoundaryIs>
        <LinearRing>
            <coordinates>
                        -25.0,30.0,0 -26.0,30.4,0 
            </coordinates>
        </LinearRing>
    </outerBoundaryIs>
</Polygon>

The coordinate string is variable length with each tuple separated by a space. Pugixml correctly finds the node tags in the larger file, but when getting the coordinate text, it will only read the first value.

Snippet of how it is being loaded/parsed:

    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file("file.xml");

    pugi::xpath_node_set name_nodes = doc.select_nodes("//name");
    for(const auto& xpath_node : name_nodes)
    {
        auto name_node = xpath_node.node();
        if(name_node.text().as_string() == "search_name"))
        {
            auto path_node = name_node.parent().select_node("//coordinates");
            if(path_node)
            {
                // Prints "-25.0,30.0,0"
                std::cout << "Coordinate string: " << path_node.node().text().as_string() << std::endl;
            }
        }
    }

Is this a limitation of pugixml, or is there a different way it can be parsed to preserve whitespace? I need to get the entire coordinate string as a string.

2
  • 1
    I'm not familiar with Pugixml, and given how few questions there are about Pugixml on StackOverflow it might go unanswered for a while. Have you tried asking on the Pugixml discussion forum? Commented Jul 14 at 22:11
  • @Eljay No, but I'll go ahead and post there. Thanks! Commented Jul 14 at 22:27

1 Answer 1

2

Xpath query was incorrect. Need to add a "." to the Xpath query to make it start at the selected node. Second query should be:

auto path_node = name_node.parent().select_node(".//coordinates");

The value that was being returned was the first occurrence of a coordinates node in the entire document, which just so happened to match.

Sign up to request clarification or add additional context in comments.

1 Comment

heh, well that explains why I wasn't able to reproduce the problem. :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.