1

we are seeing that the address of all the children at the same level are the same, this is hurting our logic as we want to store the addresses of xml nodes for later processing. here is the sample xml we are using and the output.

xml:

<?xml version="1.0"?>
<network>
    <animation clip="idle" flags="loop" />
    <animation clip="run" flags="loop" />
    <animation clip="attack" />
    <?include transitions.xml?>
</network>

code:

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include "pugixml.hpp"
#include "tinyformat.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <streambuf>
#include <tuple>
#include <algorithm>
#include <cctype>
#include <string>

int main( int ac, char **av )
{
    try {
        std::ifstream t("/home/rk/character.xml");
        std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
        std::string xml = str;
        pugi::xml_document *doc = new pugi::xml_document();
        auto result = doc->load_buffer_inplace ( const_cast <char*> ( xml.c_str() ), xml.size() );
        if ( result.status != pugi::xml_parse_status::status_ok ) {
            throw std::runtime_error ( std::string ( std::string( "Error parsing the xml: " )
                        +
                        std::string ( result.description() ) ));
        }
        std::cerr<<" children: " << std::endl;
        std::cerr<<" --------: " << std::endl;
        for ( pugi::xml_node_iterator it = doc->first_child().begin(); it != doc->first_child().end(); ++it ) {
            pugi::xml_node *element = &(*it);
            std::cerr << " type : " << element->name() 
                << " name : " << element->attribute("clip").as_string() 
                << " address : " << element 
                << std::endl;
        }
        std::cerr<<" --------: " << std::endl;
        return 0;
    } catch ( std::exception &ex ) {
        std::cerr<< " main exited with exception : " << ex.what() << std::endl;
        return -1;
    }
}

output:

children: 
 --------: 
 type : animation name : idle address : 0x7ffde2386cc0
 type : animation name : run address : 0x7ffde2386cc0
 type : animation name : attack address : 0x7ffde2386cc0 <------ i was expecting different addresses
acraig5075
  • 10,588
  • 3
  • 31
  • 50
Ravikumar Tulugu
  • 1,702
  • 2
  • 18
  • 40

1 Answers1

0

I believe this is because of how pugi::xml_node_iterator is implemented.

If you look at the source of it's operator++ you'll see it modifies some state and then returns itself.

PUGI__FN const xml_node_iterator& xml_node_iterator::operator++()
{
    assert(_wrap._root);
    _wrap._root = _wrap._root->next_sibling;
    return *this;
}

So in your loop you're getting the address of the same instance each time and your output is then expected. (Of course entirely unexpected when compared to the more familiar std::iterator which works differently.)

acraig5075
  • 10,588
  • 3
  • 31
  • 50