Question

When I run this it prints XML loaded and Dimensions loaded and then prints Segmentation Fault (core dumped). I copied an example I saw in the manual for RapidXML and a similar example was given on StackOverflow. Whats wrong with my implementation?

data.xml

<?xml version="1.0"?>
<dimensions>
    <x>400</x>
    <y>400</y>
    <z>1</z>
</dimensions>

map.h

#ifndef MAP_H_
#define MAP_H_
class map{
  public:
    std::vector<int> get_xml();
};
#endif

map.cpp

#include <vector>
#include <iostream>  
#include "../lib/rapidxml.hpp"
#include "../lib/rapidxml_print.hpp"
#include "../lib/rapidxml_utils.hpp"

using std::vector;
using std::cout;
using std::endl;
using namespace rapidxml;

vector<int> map::get_xml(){
    file<> xmlFile("res/data.xml");
    cout<<"XML loaded"<<endl;
    xml_document<> doc;
    xml_node<> *dims=doc.first_node("dimensions");
    vector<int> values;
    cout<<"Dimensions loaded"<<endl;

    for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){//breaks here
        cout<<"Looping through attributes"<<endl;
        values.push_back(atoi(dim->value()));
    }
    cout<<"All values loaded"<<endl;
    return values;
}

main.cpp

#include <vector>
#include "map.h"
map Map;
int main(int argc, char** argv){
    std::vector<int> dims=Map.get_xml();
    ...
}
Was it helpful?

Solution

Looks like you need to call doc.parse<>(); on your input.

Try this:

vector<int> map::get_xml(){
    file<> xmlFile("res/data.xml");
    cout<<"XML loaded"<<endl;
    xml_document<> doc;
    doc.parse<0>(xmlFile.data());
    xml_node<> *dims=doc.first_node("dimensions");
    vector<int> values;
    cout<<"Dimensions loaded"<<endl;

    for(xml_node<> *dim=dims->first_node();dim;dim->next_sibling()){//breaks here
        cout<<"Looping through attributes"<<endl;
        values.push_back(atoi(dim->value()));
    }
    cout<<"All values loaded"<<endl;
    return values;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top