rapidXML fails to save XML doc properly

I'm trying to write a wrapper for rapidXML in order to use it in my projects however I've run into a curious bug that's preventing me from properly saving the XML documents I'm working with. The gist of it is: simply parsing a very simple xml doc and then saving the same document to the same file (without modifying it) creates a mostly correct output except for scrambling a few node closures:

<?xml version="1.0" encoding="utf-8"?>
<data>
        <words>
                <determiner>
                        <the/>
                        <this/>
                </determiner>
                <preposition>
                        <in/>
                </preposition>
                <conjunction/>
                <verb>
                        <must/>
                </verb>
                <adverb/>
                <noun>
                        <Carl/>
                        <mountains/>
                </noun>
                <adjective/>
                <pronoun/>
        </words>
</data>


turns into(note the </closures> for <data> and <words>):
<?xml version="1.0" encoding="utf-8"?>
<data>
        <words>
                <determiner>
                        <the/>
                        <this/>
                </determiner>
                <preposition>
                        <in/>
                </preposition>
                <conjunction/>
                <verb>
                        <must/>
                </verb>
                <adverb/>
                <noun>
                        <Carl/>
                        <mountains/>
                </noun>
                <adjective/>
                <pronoun/>
        </rmine>
</              </>


The code doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool rXMLwrapper::parse(){
    try{
        std::ifstream file(filepath.c_str());
        if(!file.is_open()){throw std::bad_alloc();}
        std::stringstream buffer;
        buffer << file.rdbuf();
        file.close();
        std::string content(buffer.str());
 
        doc.parse<0>(&content[0]);
        root=doc.first_node();
    }catch(std::exception& e){
        std::cout << "\nError parsing XML document\n" << e.what() << '\n';
        return 0;
    }
    std::cout << doc;
    return 1;
}


1
2
3
4
5
6
7
void rXMLwrapper::save(){
    std::ofstream file_out(filepath.c_str());
    file_out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl;
    file_out << doc;
    file_out.close();
    return;
}


1
2
3
4
5
6
7
#include "rXMLwrapper.h"
int main()
{
    rXMLwrapper doc("data\\xmlfile.xml");
    doc.parse();
    doc.save();
}


I'm using this patch for rapidXML: http://pastebin.com/aGFw3enU

I'm totally lost as to what might be causing this.
As a foot note: I'm almost certain it's not an issue with the parse() function.
If I parse() the document and then call std::cout<<doc; it prints the document perfectly fine to the console, but for some reason std::ofstream<<doc; causes issues.
Last edited on
Try using the rapidXML API to load your file:
1
2
3
4
5
6
7
bool rXMLwrapper::parse(){
    try{
        file<> in(filepath.c_str());  // #include rapidxml_utils.hpp
        doc.parse<0>(in.data());
        root=doc.first_node();
    }catch(std::exception& e){
. . .
Topic archived. No new replies allowed.