3

I am using PugiXml library for XML related operations.

My XML File:

<CurrentStatus>
    <Time Stamp= "12:30">
        <price>100</price>
        <amount>1</amount>
    </Time>

    <Time Stamp= "14:50">
        <price>10</price>
        <amount>5</amount>
    </Time> 

    <Time Stamp= "16:30">
        <price>10</price>
        <amount>5</amount>
    </Time>     
</CurrentStatus>

For testing purpose, I am giving a hard-coded value that I want to delete the node Time with attribute Stamp = 14:50.

XML Node Removal Code: I used this SO Question as reference to remove a node (name = Time and Attribute = 14:50).

for (xml_node child = doc.child("CurrentStatus").first_child(); child; )
{
    xml_node next = child.next_sibling();       
    string attributeValue = child.attribute("Stamp").as_string();

    if (attributeValue == "14:50")
    {
        cout << "delete" << endl;
        child.parent().remove_child(child);
    }

    child = next;
}

Question: The above code runs without any error. It even enters into the if-statement but why the original XML file remains same after the execution?

PS: I am sure that the root node and the XML document is getting read properly in General since I am able to display the XML structure on the console.

2
  • Do you get an error while overwriting the XML file with the new content? Commented Oct 15, 2018 at 12:33
  • @king_nak: no, there was no error. Commented Oct 15, 2018 at 13:15

1 Answer 1

0

I think that the problem was in Saving.

I executed the following save statement after the if-condition and it worked.

doc.save_file("D:/myfile.xml");
Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense. You load a XML file into memory, modify the DOM in memory, and then have to write the new content from memory to the file. That's the common working principle. Changes to you data structures usually aren't automagically written back to the source
@king_nak: Yes, since it was not mentioned in the documentation as well as in the SO answer so, I missed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.