How to append new Child node to existing file using Rapidxml in c++

167 Views Asked by At

Below is my code to create an xml using Rapidxml.

...

        std::ofstream theFile(pathToCheck);
        xml_document<> doc;

        xml_node<>* root = doc.allocate_node(node_element, "ApplicationsDetails");
        doc.append_node(root);

        //child Node : <ApplicationData>
        xml_node<>* child = doc.allocate_node(rapidxml::node_element, "ApplicationInfo");
        

        child->append_attribute(doc.allocate_attribute("ApplicationID", "1234"));
        child->append_attribute(doc.allocate_attribute("ApplicationTitle", "Saha"));

        root->append_node(child);
        theFile << doc;

        theFile.close();

        doc.clear();

...

And below are the nodes saved to file. ...

  <ApplicationsDetails>
                <ApplicationInfo ApplicationID="1234" ApplicationTitle="Saha" />
   </ApplicationsDetails>

...

Now, I want to append new child node to that xml, like I have to open that file and read the nodes and append new child node like below, so total output should be ...

<ApplicationsDetails>
        <ApplicationInfo ApplicationID="1234" ApplicationTitle="Saha" />
        <ApplicationInfo ApplicationID="5678" ApplicationTitle="nagg" />
    </ApplicationsDetails>

...

I tried this, but somehow , nodes are not appended properly, can any one suggest the code?

0

There are 0 best solutions below