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.

skm
  • 4,300
  • 7
  • 35
  • 82

1 Answers1

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");
skm
  • 4,300
  • 7
  • 35
  • 82
  • 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 Oct 15 '18 at 13:20
  • @king_nak: Yes, since it was not mentioned in the documentation as well as in the SO answer so, I missed it. – skm Oct 15 '18 at 13:31