I need to replace all nodes with others nodes.
My current node:
<str name="author">Brad Mc</str>
I need to replace it with this node:
<author>Brad Mc<author>
I have this code to replace all nodes with others nodes:
IXMLNode* xResultNode = XMLDocument1->DocumentElement->ChildNodes->FindNode("result");
IXMLNode* xDocNode;
IXMLNode* xFieldNode;
IXMLNode* xNewFieldNode;
// <result>
for (int i = 0; i < xResultNode->ChildNodes->Count - 1; i) {
// <doc>
xDocNode = xResultNode->ChildNodes->Get(i);
int count = xDocNode->ChildNodes->Count;
for (int j = 0; j < count - 1; j++) {
// <field>
xFieldNode = xDocNode->ChildNodes->Get(j);
String FieldName = xFieldNode->Attributes["name"];
String FieldText = xFieldNode->Text;
// Create new Node / modify node
xNewFieldNode = xDocNode->AddChild(FieldName);
xNewFieldNode->SetText(FieldText);
// I need to replace xFieldNode with xNewFieldNode
// how to do that?
}
}
XMLDocument1->SaveToFile("./ResponseOutPut.xml");
First, this code is full of memory leaks and invalid memory access. IXMLNode
is a reference counted interface, but you are not managing the reference counts correctly. You need to replace IXMLNode*
with _di_IXMLNode
and let it manage the reference counts for you.
Second, to replace an entire node with a new node, you can use the parent node's ChildNodes->ReplaceNode()
method. You can use the owning document's CreateNode()
method to create the new node.