From: http://bbs.csdn.net/topics/300026982
The included library files remain unchanged: # import "msxml3.dll"
Namespace unchanged: Using namespace msxml2;
The sample code is as follows:
// Initialization
Coinitialize (null );
// Create a document
Msxml2: ixmldomdocument2ptr pxmldoc = NULL;
Pxmldoc. createinstance ("msxml2.domdocument. 3.0 ");
// Create the Processing Instruction
Msxml2: ixmldomprocessinginstructionptr pxmlproc = NULL;
Pxmlproc = pxmldoc-> createprocessinginstruction ("XML", "version = '1. 0' encoding = 'utf-8 '");
// Insert the Processing Instruction
_ Variant_t vnullval;
Vnullval. Vt = vt_null;
Pxmldoc-> insertbefore (pxmlproc, vnullval );
// Create a root node
_ Variant_t varnodetype (short) msxml2: node_element );
Msxml2: ixmldomnodeptr pxmlnoderoot = NULL;
Pxmlnoderoot = pxmldoc-> createnode (varnodetype, _ T ("root"), _ T (""));
// Add the root node
Pxmldoc-> appendchild (pxmlnoderoot );
// Create and add a subnode
Msxml2: ixmldomnodeptr pxmlnodenode = NULL;
Pxmlnodenode = pxmlnoderoot-> appendchild (pxmldoc-> createelement (_ T ("node1 ")));
Pxmlnodenode = pxmlnoderoot-> appendchild (pxmldoc-> createelement (_ T ("node2 ")));
// Create and set the attributes of the subnode (name, gender, and age)
Msxml2: ixmldomattributeptr pxmlattr = NULL;
Pxmlattr = pxmldoc-> createattribute (_ T ("name "));
Pxmlattr-> nodetypedvalue = "Wang Jin ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmldoc-> createattribute (_ T ("sex "));
Pxmlattr-> nodetypedvalue = "male ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmldoc-> createattribute (_ T ("Age "));
Pxmlattr-> nodetypedvalue = "80 ";
Pxmlnodenode-> attributes-> setnameditem (pxmlattr );
// Create a lower-level Element Node
Msxml2: ixmldomelementptr pxmlele = NULL;
Pxmlele = pxmldoc-> createelement (_ T ("file "));
Pxmlele-> appendchild (pxmldoc-> createtextnode ("element "));
// Add element nodes
Pxmlnodenode-> appendchild (pxmlele );
/*
// Create a lower-level Element Node
Msxml2: ixmldomelementptr pxmlelesub = NULL;
Pxmlelesub = pxmldoc-> createelement (_ T ("file "));
Pxmlelesub-> appendchild (pxmldoc-> createtextnode ("subelement "));
// Add a lower-level Element Node
Pxmlele-> appendchild (pxmlelesub );
*/
// Save the document
Pxmldoc-> Save (_ T ("D: \ test. xml "));
Couninitialize ();
Conclusion: when no element exists under a node, the node is not expanded.
Delete a node:
Pnodelist-> get_item (I, & pnode );
Pnod-> removechild (pnode, & moldnode );
Moldnode-> release ();
When I used it in the initoxml project, I found that the removechild function only receives one parameter: pnode
In addition, when all nodes are deleted from an index, I in get_item (I, & pnode) cannot increase progressively because each time a node is deleted, the subsequent nodes will be moved to the front.