0

Iam having problem with rapidxml. Code below gives me output: http://pastebin.com/352MxuQY
But when I remove loop or "{}" where Iam appending node I get good output: http://pastebin.com/H0YQGwV1
Why is this happening?

rapidxml::xml_document<> andDoc;

ifstream andfile(PATH+XMLNAME);
vector<char> buffer((istreambuf_iterator<char>(andfile)), istreambuf_iterator<char>( ));
buffer.push_back('\0');
cout<<&buffer[0]<<endl; 
andDoc.parse<0>(&buffer[0]); 
xml_node<>* cos = andDoc.first_node("Data")->first_node("Classifiers");
xml_node<>* klda = andDoc.first_node("Data")->first_node("Kldas");

for(int i=0;i<1;i++)
{

    rapidxml::xml_document<> doc;

    ifstream myfile(cPATH+"0\\c.xml");
    vector<char> buffer2((istreambuf_iterator<char>(myfile)), istreambuf_iterator<char>( ));
    buffer2.push_back('\0');
    cout<<&buffer2[0]<<endl; 
    doc.parse<0>(&buffer2[0]); 
    xml_node<>* cl = doc.first_node();
    xml_node<>* asd = doc.clone_node(cl);

    cos->append_node(asd);

    myfile.close();

}
std::ofstream file(PATH+XMLNAME);
if (file.is_open())
{
    file.clear();
    file << andDoc;
    file.close();
}

1 Answers1

0

Somewhat late, but here's what I think is happening: RapidXML's clone_node doesn't work the way you think it does.

From the docs, with some added emphasis...


Synopsis

xml_node<Ch>* clone_node(const xml_node< Ch > *source, xml_node< Ch > *result=0);

Description

Clones an xml_node and its hierarchy of child nodes and attributes. Nodes and attributes are allocated from this memory pool. Names and values are not cloned, they are shared between the clone and the source. Result node can be optionally specified as a second parameter, in which case its contents will be replaced with cloned source node. This is useful when you want to clone entire document.


So, what's happening is that the names and values of the cloned node go out of scope (and are thus lost) when the xml_document 'doc' goes out of scope at the end of the for loop. Given this restriction, I can't honestly see what use clone_node is.

I'm not sure how best to fix this - it depends on your real-world requirements, but you might have to somehow keep all source docs in scope until the final output is created.

Roddy
  • 66,617
  • 42
  • 165
  • 277